001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.xjc.reader;
038:
039: import java.lang.reflect.Constructor;
040: import java.lang.reflect.InvocationTargetException;
041: import java.util.HashMap;
042: import java.util.Map;
043:
044: import com.sun.tools.xjc.ErrorReceiver;
045: import com.sun.tools.xjc.model.Model;
046:
047: /**
048: * Holds all the binding related singleton components in a "ring",
049: * and let you access those components, creating them as necessary.
050: *
051: * <p>
052: * A {@link Ring} is local to a thread,
053: * and only one instanceof {@link Ring} can be active at any given time.
054: *
055: * Use {@link #begin()} and {@link #end(Ring)} to start/end a ring scope.
056: * Inside a scope, use {@link #get()} to obtain the instance.
057: *
058: * <p>
059: * When a {@link Model} is built by the reader, an active {@link Ring} scope
060: * is assumed.
061: *
062: *
063: * <h2>Components in Ring</h2>
064: * <p>
065: * Depending on the schema language we are dealing with, different
066: * components are in the model. But at least the following components
067: * are in the ring.
068: *
069: * <ul>
070: * <li>{@link ErrorReceiver}
071: * </ul>
072: *
073: * @author Kohsuke Kawaguchi
074: */
075: public final class Ring {
076:
077: private final Map<Class, Object> components = new HashMap<Class, Object>();
078:
079: private static final ThreadLocal<Ring> instances = new ThreadLocal<Ring>() {
080: public Ring initialValue() {
081: return new Ring();
082: }
083: };
084:
085: private Ring() {
086: }
087:
088: public static <T> void add(Class<T> clazz, T instance) {
089: assert !get().components.containsKey(clazz);
090: get().components.put(clazz, instance);
091: }
092:
093: public static <T> void add(T o) {
094: add((Class<T>) o.getClass(), o);
095: }
096:
097: public static <T> T get(Class<T> key) {
098: T t = (T) get().components.get(key);
099: if (t == null) {
100: try {
101: Constructor<T> c = key.getDeclaredConstructor();
102: c.setAccessible(true);
103: t = c.newInstance();
104: if (!get().components.containsKey(key))
105: // many components register themselves.
106: add(key, t);
107: } catch (InstantiationException e) {
108: throw new Error(e);
109: } catch (IllegalAccessException e) {
110: throw new Error(e);
111: } catch (NoSuchMethodException e) {
112: throw new Error(e);
113: } catch (InvocationTargetException e) {
114: throw new Error(e);
115: }
116: }
117:
118: assert t != null;
119: return t;
120: }
121:
122: /**
123: * A {@link Ring} instance is associated with a thread.
124: */
125: public static Ring get() {
126: return instances.get();
127: }
128:
129: /**
130: * Starts a new scope.
131: */
132: public static Ring begin() {
133: Ring r = instances.get();
134: instances.set(new Ring());
135: return r;
136: }
137:
138: /**
139: * Ends a scope.
140: */
141: public static void end(Ring old) {
142: instances.set(old);
143: }
144: }
|