KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > dependency > AbstractKernelControllerContext


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.kernel.plugins.dependency;
23
24 import java.security.AccessControlContext JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedAction JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.Stack JavaDoc;
30
31 import org.jboss.beans.info.spi.BeanInfo;
32 import org.jboss.beans.metadata.spi.BeanMetaData;
33 import org.jboss.beans.metadata.spi.MetaDataVisitor;
34 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
35 import org.jboss.dependency.plugins.AbstractControllerContext;
36 import org.jboss.dependency.plugins.AbstractDependencyInfo;
37 import org.jboss.dependency.spi.Controller;
38 import org.jboss.dependency.spi.ControllerMode;
39 import org.jboss.dependency.spi.ControllerState;
40 import org.jboss.dependency.spi.DependencyInfo;
41 import org.jboss.dependency.spi.DependencyItem;
42 import org.jboss.kernel.Kernel;
43 import org.jboss.kernel.spi.dependency.KernelController;
44 import org.jboss.kernel.spi.dependency.KernelControllerContext;
45 import org.jboss.kernel.spi.metadata.MutableMetaDataContext;
46 import org.jboss.repository.spi.MetaDataContext;
47 import org.jboss.util.JBossStringBuilder;
48
49 /**
50  * Controller context.
51  *
52  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
53  * @version $Revision: 58072 $
54  */

55 public class AbstractKernelControllerContext extends AbstractControllerContext implements KernelControllerContext
56 {
57    /**
58     * The default actions
59     */

60    private static final KernelControllerContextActions actions = KernelControllerContextActions.getInstance();
61
62    /**
63     * The no instantiate actions
64     */

65    private static final KernelControllerContextActions noInstantiate = KernelControllerContextActions.getNoInstantiate();
66
67    /**
68     * The BeanInfo
69     */

70    protected BeanInfo info;
71
72    /**
73     * The meta data
74     */

75    protected BeanMetaData metaData;
76
77    /**
78     * The access control context
79     */

80    protected AccessControlContext JavaDoc accessContext;
81
82    /**
83     * Did we do a describeVisit
84     */

85    protected boolean isDescribeProcessed;
86
87    /**
88     * Create an abstract controller context
89     *
90     * @param info the bean info
91     * @param metaData the meta data
92     * @param target any target object
93     */

94    public AbstractKernelControllerContext(BeanInfo info, BeanMetaData metaData, Object JavaDoc target)
95    {
96       super(metaData.getName(), target == null ? actions : noInstantiate, new AbstractDependencyInfo(), target);
97       this.info = info;
98       this.metaData = metaData;
99       ControllerMode mode = metaData.getMode();
100       if (mode != null)
101          setMode(mode);
102       if (System.getSecurityManager() != null)
103          accessContext = AccessController.getContext();
104    }
105
106    public Kernel getKernel()
107    {
108       KernelController controller = (KernelController) getController();
109       if (controller == null)
110          throw new IllegalStateException JavaDoc("Context is not installed in controller");
111       return controller.getKernel();
112    }
113
114    public BeanInfo getBeanInfo()
115    {
116       return info;
117    }
118
119    /**
120     * Set the bean info
121     *
122     * @param info the bean info
123     */

124    public void setBeanInfo(BeanInfo info)
125    {
126       this.info = info;
127       infoprocessMetaData();
128       flushJBossObjectCache();
129    }
130
131    public BeanMetaData getBeanMetaData()
132    {
133       return metaData;
134    }
135
136    public void toString(JBossStringBuilder buffer)
137    {
138       if (metaData != null)
139          buffer.append(" metadata=").append(metaData);
140       super.toString(buffer);
141    }
142
143    public void setController(Controller JavaDoc controller)
144    {
145       super.setController(controller);
146       preprocessMetaData();
147    }
148
149    /**
150     * Preprocess the metadata for this context
151     */

152    protected void preprocessMetaData()
153    {
154       if (metaData == null)
155          return;
156       PreprocessMetaDataVisitor visitor = new PreprocessMetaDataVisitor(metaData);
157       AccessController.doPrivileged(visitor);
158    }
159
160    /**
161     * Preprocess the metadata for this context
162     */

163    protected void infoprocessMetaData()
164    {
165       if (info == null)
166       {
167          removeClassContextReference();
168          return;
169       }
170       if (isDescribeProcessed) return;
171       DescribedMetaDataVisitor visitor = new DescribedMetaDataVisitor(metaData);
172       AccessController.doPrivileged(visitor);
173       isDescribeProcessed = true;
174    }
175
176    public MutableMetaDataContext getMetaDataContext()
177    {
178       if (info != null)
179       {
180          try
181          {
182             return (MutableMetaDataContext)info.getMetaDataContext();
183          }
184          catch (ClassCastException JavaDoc e)
185          {
186             throw new RuntimeException JavaDoc("MetaDataContext must be an instance of MutableMetaDataContext");
187          }
188       }
189       return null;
190    }
191
192    public void setMetaDataContext(MetaDataContext mctx)
193    {
194       info = info.getInstanceInfo();
195       info.setMetaDataContext(mctx);
196    }
197
198    /**
199     * Get the access control context of the code that created this context.<p>
200     * <p/>
201     * This will be null when there is no security manager.
202     *
203     * @return any access control context
204     */

205    protected AccessControlContext JavaDoc getAccessControlContext()
206    {
207       return accessContext;
208    }
209
210    private void removeClassContextReference()
211    {
212       DependencyInfo dependencyInfo = getDependencyInfo();
213       if (dependencyInfo != null)
214       {
215          Set JavaDoc dependencys = dependencyInfo.getIDependOn(ClassContextDependencyItem.class);
216          for(Iterator JavaDoc it = dependencys.iterator(); it.hasNext();)
217          {
218             DependencyItem di = (DependencyItem) it.next();
219             di.unresolved(getController());
220          }
221       }
222    }
223
224    protected abstract class AbstractMetaDataVistor implements MetaDataVisitor, PrivilegedAction JavaDoc<Object JavaDoc>
225    {
226       /**
227        * The current context for when the dependencies are required
228        */

229       protected ControllerState contextState = ControllerState.INSTANTIATED;
230
231       /**
232        * The metadata
233        */

234       protected BeanMetaData bmd;
235
236       /**
237        * Visited branch stack
238        */

239       protected Stack JavaDoc<MetaDataVisitorNode> visitorNodeStack;
240
241       protected AbstractMetaDataVistor(BeanMetaData bmd)
242       {
243          this.bmd = bmd;
244          this.visitorNodeStack = new Stack JavaDoc<MetaDataVisitorNode>();
245       }
246
247       public void initialVisit(MetaDataVisitorNode node)
248       {
249          visitorNodeStack.push(node);
250          try
251          {
252             internalInitialVisit(node);
253          }
254          finally
255          {
256             visitorNodeStack.pop();
257          }
258       }
259
260       public void describeVisit(MetaDataVisitorNode node)
261       {
262          visitorNodeStack.push(node);
263          try
264          {
265             internalDescribeVisit(node);
266          }
267          finally
268          {
269             visitorNodeStack.pop();
270          }
271       }
272
273       protected void internalInitialVisit(MetaDataVisitorNode node)
274       {
275       }
276
277       protected void internalDescribeVisit(MetaDataVisitorNode node)
278       {
279       }
280
281       public KernelControllerContext getControllerContext()
282       {
283          return AbstractKernelControllerContext.this;
284       }
285
286       public ControllerState getContextState()
287       {
288          return contextState;
289       }
290
291       public void addDependency(DependencyItem dependency)
292       {
293          getDependencyInfo().addIDependOn(dependency);
294       }
295
296       public void setContextState(ControllerState contextState)
297       {
298          this.contextState = contextState;
299       }
300
301       public Stack JavaDoc<MetaDataVisitorNode> visitorNodeStack()
302       {
303          return visitorNodeStack;
304       }
305
306    }
307
308    /**
309     * A visitor for the metadata that looks for dependencies.
310     */

311    protected class PreprocessMetaDataVisitor extends AbstractMetaDataVistor
312    {
313       /**
314        * Create a new MetaDataVisitor.
315        *
316        * @param bmd the bean metadata
317        */

318       public PreprocessMetaDataVisitor(BeanMetaData bmd)
319       {
320          super(bmd);
321       }
322
323       /**
324        * Visit the bean metadata node, this is the starting point
325        */

326       public Object JavaDoc run()
327       {
328          bmd.initialVisit(this);
329          visitorNodeStack = null;
330          return null;
331       }
332
333       /**
334        * Visit a node
335        *
336        * @param node the node
337        */

338       protected void internalInitialVisit(MetaDataVisitorNode node)
339       {
340          boolean trace = log.isTraceEnabled();
341          if (trace)
342             log.trace("Initial visit node " + node);
343
344          // Visit the children of this node
345
Iterator JavaDoc children = node.getChildren();
346          if (children != null)
347          {
348             ControllerState restoreState = contextState;
349             while (children.hasNext())
350             {
351                MetaDataVisitorNode child = (MetaDataVisitorNode) children.next();
352                try
353                {
354                   child.initialVisit(this);
355                }
356                finally
357                {
358                   contextState = restoreState;
359                }
360             }
361          }
362       }
363
364    }
365
366    /**
367     * A visitor for the metadata that looks for dependencies.
368     */

369    protected class DescribedMetaDataVisitor extends AbstractMetaDataVistor
370    {
371       /**
372        * Create a new MetaDataVisitor.
373        *
374        * @param bmd the bean meta data
375        */

376       public DescribedMetaDataVisitor(BeanMetaData bmd)
377       {
378          super(bmd);
379       }
380
381       /**
382        * Visit the bean metadata node, this is the starting point
383        */

384       public Object JavaDoc run()
385       {
386          bmd.describeVisit(this);
387          visitorNodeStack = null;
388          return null;
389       }
390
391       /**
392        * Visit a node
393        *
394        * @param node the node
395        */

396       protected void internalDescribeVisit(MetaDataVisitorNode node)
397       {
398          boolean trace = log.isTraceEnabled();
399          if (trace)
400             log.trace("Describe visit node " + node);
401
402          // Visit the children of this node
403
Iterator JavaDoc children = node.getChildren();
404          if (children != null)
405          {
406             ControllerState restoreState = contextState;
407             while (children.hasNext())
408             {
409                MetaDataVisitorNode child = (MetaDataVisitorNode) children.next();
410                try
411                {
412                   child.describeVisit(this);
413                }
414                finally
415                {
416                   contextState = restoreState;
417                }
418             }
419          }
420       }
421    }
422
423 }
Popular Tags