webidor/src/main/java/cz/xelfi/quoridor/webidor/JAXBContextResolver.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 30 Aug 2009 14:44:49 +0200
changeset 49 75074e02f345
parent 35 2e85dd878f04
child 77 d574ac6e44cc
permissions -rw-r--r--
At least we are able to generate the index page
jtulach@35
     1
/*
jtulach@35
     2
 *
jtulach@35
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@35
     4
 * 
jtulach@35
     5
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
jtulach@35
     6
 * 
jtulach@35
     7
 * The contents of this file are subject to the terms of either the GNU
jtulach@35
     8
 * General Public License Version 2 only ("GPL") or the Common Development
jtulach@35
     9
 * and Distribution License("CDDL") (collectively, the "License").  You
jtulach@35
    10
 * may not use this file except in compliance with the License. You can obtain
jtulach@35
    11
 * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html
jtulach@35
    12
 * or jersey/legal/LICENSE.txt.  See the License for the specific
jtulach@35
    13
 * language governing permissions and limitations under the License.
jtulach@35
    14
 * 
jtulach@35
    15
 * When distributing the software, include this License Header Notice in each
jtulach@35
    16
 * file and include the License file at jersey/legal/LICENSE.txt.
jtulach@35
    17
 * Sun designates this particular file as subject to the "Classpath" exception
jtulach@35
    18
 * as provided by Sun in the GPL Version 2 section of the License file that
jtulach@35
    19
 * accompanied this code.  If applicable, add the following below the License
jtulach@35
    20
 * Header, with the fields enclosed by brackets [] replaced by your own
jtulach@35
    21
 * identifying information: "Portions Copyrighted [year]
jtulach@35
    22
 * [name of copyright owner]"
jtulach@35
    23
 * 
jtulach@35
    24
 * Contributor(s):
jtulach@35
    25
 * 
jtulach@35
    26
 * If you wish your version of this file to be governed by only the CDDL or
jtulach@35
    27
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
jtulach@35
    28
 * elects to include this software in this distribution under the [CDDL or GPL
jtulach@35
    29
 * Version 2] license."  If you don't indicate a single choice of license, a
jtulach@35
    30
 * recipient has the option to distribute your version of this file under
jtulach@35
    31
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
jtulach@35
    32
 * its licensees as provided above.  However, if you add GPL Version 2 code
jtulach@35
    33
 * and therefore, elected the GPL Version 2 license, then the option applies
jtulach@35
    34
 * only if the new code is made subject to such option by the copyright
jtulach@35
    35
 * holder.
jtulach@35
    36
 */
jtulach@35
    37
jtulach@35
    38
package cz.xelfi.quoridor.webidor;
jtulach@35
    39
jtulach@35
    40
import com.sun.jersey.api.json.JSONConfiguration;
jtulach@35
    41
import com.sun.jersey.api.json.JSONJAXBContext;
jtulach@35
    42
import java.util.Arrays;
jtulach@35
    43
import java.util.HashSet;
jtulach@35
    44
import java.util.Set;
jtulach@35
    45
import javax.ws.rs.ext.ContextResolver;
jtulach@35
    46
import javax.ws.rs.ext.Provider;
jtulach@35
    47
import javax.xml.bind.JAXBContext;
jtulach@35
    48
jtulach@35
    49
/**
jtulach@35
    50
 *
jtulach@35
    51
 * @author japod
jtulach@35
    52
 */
jtulach@35
    53
@Provider
jtulach@35
    54
public final class JAXBContextResolver implements ContextResolver<JAXBContext> {
jtulach@35
    55
    
jtulach@35
    56
    private final JAXBContext context;
jtulach@35
    57
    
jtulach@35
    58
    private final Set<Class> types;
jtulach@35
    59
    
jaroslav@49
    60
    private final Class[] cTypes = {Game.class, GameId.class, GameResult.class };
jtulach@35
    61
    
jtulach@35
    62
    public JAXBContextResolver() throws Exception {
jtulach@35
    63
        this.types = new HashSet(Arrays.asList(cTypes));
jtulach@35
    64
        this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
jtulach@35
    65
    }
jtulach@35
    66
    
jtulach@35
    67
    public JAXBContext getContext(Class<?> objectType) {
jtulach@35
    68
        return (types.contains(objectType)) ? context : null;
jtulach@35
    69
    }
jtulach@35
    70
}