View Javadoc
1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.tools.internal.xjc.reader;
27  
28  import java.lang.reflect.Constructor;
29  import java.lang.reflect.InvocationTargetException;
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  import com.sun.tools.internal.xjc.ErrorReceiver;
34  import com.sun.tools.internal.xjc.model.Model;
35  
36  /**
37   * Holds all the binding related singleton components in a "ring",
38   * and let you access those components, creating them as necessary.
39   *
40   * <p>
41   * A {@link Ring} is local to a thread,
42   * and only one instanceof {@link Ring} can be active at any given time.
43   *
44   * Use {@link #begin()} and {@link #end(Ring)} to start/end a ring scope.
45   * Inside a scope, use {@link #get()} to obtain the instance.
46   *
47   * <p>
48   * When a {@link Model} is built by the reader, an active {@link Ring} scope
49   * is assumed.
50   *
51   *
52   * <h2>Components in Ring</h2>
53   * <p>
54   * Depending on the schema language we are dealing with, different
55   * components are in the model. But at least the following components
56   * are in the ring.
57   *
58   * <ul>
59   *  <li>{@link ErrorReceiver}
60   * </ul>
61   *
62   * @author Kohsuke Kawaguchi
63   */
64  public final class Ring {
65  
66      private final Map<Class,Object> components = new HashMap<Class,Object>();
67  
68      private static final ThreadLocal<Ring> instances = new ThreadLocal<Ring>();
69  
70      private Ring() {}
71  
72      public static <T> void add( Class<T> clazz, T instance ) {
73          assert !get().components.containsKey(clazz);
74          get().components.put(clazz,instance);
75      }
76  
77      public static <T> void add( T o ) {
78          add((Class<T>)o.getClass(),o);
79      }
80  
81      public static <T> T get( Class<T> key ) {
82          T t = (T)get().components.get(key);
83          if(t==null) {
84              try {
85                  Constructor<T> c = key.getDeclaredConstructor();
86                  c.setAccessible(true);
87                  t = c.newInstance();
88                  if(!get().components.containsKey(key))
89                      // many components register themselves.
90                      add(key,t);
91              } catch (InstantiationException e) {
92                  throw new Error(e);
93              } catch (IllegalAccessException e) {
94                  throw new Error(e);
95              } catch (NoSuchMethodException e) {
96                  throw new Error(e);
97              } catch (InvocationTargetException e) {
98                  throw new Error(e);
99              }
100         }
101 
102         assert t!=null;
103         return t;
104     }
105 
106     /**
107      * A {@link Ring} instance is associated with a thread.
108      */
109     public static Ring get() {
110         return instances.get();
111     }
112 
113     /**
114      * Starts a new scope.
115      */
116     public static Ring begin() {
117         Ring r = null;
118         synchronized (instances) {
119             r = instances.get();
120             instances.set(new Ring());
121         }
122         return r;
123     }
124 
125     /**
126      * Ends a scope.
127      */
128     public static void end(Ring old) {
129         synchronized (instances) {
130             instances.remove();
131             instances.set(old);
132         }
133     }
134 }