KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > microcontainer > ServiceControllerContext


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.system.microcontainer;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.dependency.plugins.AbstractControllerContext;
32 import org.jboss.dependency.spi.Controller;
33 import org.jboss.dependency.spi.ControllerMode;
34 import org.jboss.dependency.spi.ControllerState;
35 import org.jboss.dependency.spi.DependencyItem;
36 import org.jboss.system.Service;
37 import org.jboss.system.ServiceContext;
38 import org.jboss.system.ServiceController;
39 import org.jboss.system.metadata.ServiceMetaData;
40 import org.jboss.system.metadata.ServiceMetaDataVisitor;
41 import org.jboss.system.metadata.ServiceMetaDataVisitorNode;
42
43 /**
44  * ServiceControllerContext.
45  *
46  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 1.1 $
48  */

49 public class ServiceControllerContext extends AbstractControllerContext
50 {
51    /** The ObjectName */
52    private ObjectName JavaDoc objectName;
53    
54    /** The service controller */
55    private ServiceController serviceController;
56    
57    /** The meta data */
58    private ServiceMetaData serviceMetaData;
59    
60    /** The service context */
61    private ServiceContext serviceContext = new ServiceContext();
62    
63    /**
64     * Create a new ServiceControllerContext.
65     *
66     * @param serviceController the service controller
67     * @param name the name of the context
68     */

69    public ServiceControllerContext(ServiceController serviceController, ObjectName JavaDoc name)
70    {
71       super(name.getCanonicalName(), ServiceControllerContextActions.getLifecycleOnly());
72       this.objectName = name;
73       serviceContext.objectName = objectName;
74       this.serviceController = serviceController;
75       setMode(ControllerMode.MANUAL);
76    }
77    
78    /**
79     * Create a new ServiceControllerContext.
80     *
81     * @param serviceController the service controller
82     * @param metaData the meta data
83     */

84    public ServiceControllerContext(ServiceController serviceController, ServiceMetaData metaData)
85    {
86       super(metaData.getObjectName().getCanonicalName(), ServiceControllerContextActions.getInstance());
87       this.objectName = metaData.getObjectName();
88       serviceContext.objectName = objectName;
89       this.serviceController = serviceController;
90       ControllerMode mode = metaData.getMode();
91       this.serviceMetaData = metaData;
92       if (mode == null)
93          setMode(ControllerMode.MANUAL);
94       else
95          setMode(mode);
96    }
97    
98    /**
99     * Create a new ServiceControllerContext.
100     *
101     * @param serviceController the service controller
102     * @param name the name
103     * @param target the target
104     */

105    public ServiceControllerContext(ServiceController serviceController, ObjectName JavaDoc name, Object JavaDoc target)
106    {
107       super(name.getCanonicalName(), ServiceControllerContextActions.getInstance());
108       this.objectName = name;
109       serviceContext.objectName = objectName;
110       this.serviceController = serviceController;
111       setTarget(target);
112       setMode(ControllerMode.MANUAL);
113    }
114
115    /**
116     * Get the ObjectName.
117     *
118     * @return the ObjectName.
119     */

120    public ObjectName JavaDoc getObjectName()
121    {
122       return objectName;
123    }
124
125    /**
126     * Get the serviceMetaData.
127     *
128     * @return the serviceMetaData.
129     */

130    public ServiceMetaData getServiceMetaData()
131    {
132       return serviceMetaData;
133    }
134
135    /**
136     * Set the serviceMetaData.
137     *
138     * @param serviceMetaData the serviceMetaData.
139     */

140    public void setServiceMetaData(ServiceMetaData serviceMetaData)
141    {
142       this.serviceMetaData = serviceMetaData;
143    }
144
145    /**
146     * Get the serviceController.
147     *
148     * @return the serviceController.
149     */

150    public ServiceController getServiceController()
151    {
152       return serviceController;
153    }
154    
155    /**
156     * Get the service proxy
157     *
158     * @return the service proxy
159     * @throws Exception for any error
160     */

161    public Service getServiceProxy() throws Exception JavaDoc
162    {
163       if (serviceContext.proxy != null)
164          return serviceContext.proxy;
165       
166       MBeanServer JavaDoc server = serviceController.getMBeanServer();
167       if (server != null)
168          serviceContext.proxy = ServiceProxy.getServiceProxy(objectName, server);
169       
170       return serviceContext.proxy;
171    }
172    
173    /**
174     * Get the service context
175     *
176     * @return the service context
177     */

178    public ServiceContext getServiceContext()
179    {
180       try
181       {
182          serviceContext.proxy = getServiceProxy();
183       }
184       catch (Exception JavaDoc ignored)
185       {
186       }
187       serviceContext.problem = getError();
188       if (getState() == ControllerState.ERROR)
189          serviceContext.state = ServiceContext.FAILED;
190       return serviceContext;
191    }
192
193    // Overridden to update the service context with any failure
194
public void install(ControllerState fromState, ControllerState toState) throws Throwable JavaDoc
195    {
196       try
197       {
198          super.install(fromState, toState);
199       }
200       catch (Throwable JavaDoc t)
201       {
202          serviceContext.problem = t;
203          serviceContext.state = ServiceContext.FAILED;
204          throw t;
205       }
206    }
207
208    // Overridden to update the service context with the installed/not installed state
209
// i.e. of the ServiceController registration
210
// Not to be confused with the microcontainer's (fully) installed state
211
public void setController(Controller JavaDoc controller)
212    {
213       super.setController(controller);
214       if (controller != null)
215       {
216          preprocessMetaData();
217          serviceContext.state = ServiceContext.INSTALLED;
218       }
219       else
220          serviceContext.state = ServiceContext.NOTYETINSTALLED;
221    }
222
223    /**
224     * Preprocess the metadata for this context
225     */

226    protected void preprocessMetaData()
227    {
228       if (serviceMetaData == null)
229          return;
230       PreprocessMetaDataVisitor visitor = new PreprocessMetaDataVisitor();
231       AccessController.doPrivileged(visitor);
232    }
233    
234    /**
235     * A visitor for the metadata that looks for dependencies.
236     */

237    protected class PreprocessMetaDataVisitor implements ServiceMetaDataVisitor, PrivilegedAction JavaDoc<Object JavaDoc>
238    {
239       /** The current context for when the dependencies are required */
240       private ControllerState contextState = ControllerState.INSTANTIATED;
241       
242       /**
243        * Visit the bean metadata node, this is the starting point
244        */

245       public Object JavaDoc run()
246       {
247          serviceMetaData.visit(this);
248          return null;
249       }
250       
251       /**
252        * Visit a node
253        *
254        * @param node the node
255        */

256       public void visit(ServiceMetaDataVisitorNode node)
257       {
258          boolean trace = log.isTraceEnabled();
259          if (trace)
260             log.trace("Visit node " + node);
261          
262          // Visit the children of this node
263
Iterator JavaDoc<? extends ServiceMetaDataVisitorNode> children = node.getChildren();
264          if (children != null)
265          {
266             ControllerState restoreState = contextState;
267             while (children.hasNext())
268             {
269                ServiceMetaDataVisitorNode child = children.next();
270                try
271                {
272                   child.visit(this);
273                }
274                finally
275                {
276                   contextState = restoreState;
277                }
278             }
279          }
280       }
281
282       public ServiceControllerContext getControllerContext()
283       {
284          return ServiceControllerContext.this;
285       }
286       
287       public ControllerState getContextState()
288       {
289          return contextState;
290       }
291       
292       public void addDependency(DependencyItem dependency)
293       {
294          getDependencyInfo().addIDependOn(dependency);
295       }
296
297       public void setContextState(ControllerState contextState)
298       {
299          this.contextState = contextState;
300       }
301    }
302 }
303
Popular Tags