KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > ServiceContext


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.system;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29
30 /**
31  * ServiceContext holds information for the Service
32  *
33  * @see Service
34  * @see ServiceMBeanSupport
35  *
36  * @author <a HREF="mailto:marc.fleury@jboss.org">marc fleury</a>
37  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
38  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
39  * @version $Revision: 58440 $
40  */

41 public class ServiceContext implements Serializable JavaDoc
42 {
43    /** @since 4.0.2 */
44    private static final long serialVersionUID = 7461263042948325633L;
45
46    /** The possible string-fied states */
47    private static final String JavaDoc[] stateNames = {
48          "INSTALLED",
49          "CONFIGURED",
50          "CREATED",
51          "RUNNING",
52          "FAILED",
53          "STOPPED",
54          "DESTROYED",
55          "NOTYETINSTALLED"
56       };
57    
58    /** Valid service states */
59    public static final int INSTALLED = 0;
60    public static final int CONFIGURED = 1;
61    public static final int CREATED = 2;
62    public static final int RUNNING = 3;
63    public static final int FAILED = 4;
64    public static final int STOPPED = 5;
65    public static final int DESTROYED = 6;
66    public static final int NOTYETINSTALLED = 7;
67    
68    /** The name of the service **/
69    public ObjectName JavaDoc objectName;
70    
71    /** State of the service **/
72    public int state = NOTYETINSTALLED;
73    
74    /** Dependent beans **/
75    public List JavaDoc<ServiceContext> iDependOn = new LinkedList JavaDoc<ServiceContext>();
76    
77    /** Beans that depend on me **/
78    public List JavaDoc<ServiceContext> dependsOnMe = new LinkedList JavaDoc<ServiceContext>();
79    
80    /** The fancy proxy to my service calls **/
81    public transient Service proxy;
82
83    /** Cause for failure */
84    public Throwable JavaDoc problem;
85    
86    public String JavaDoc getStateString()
87    {
88       return getStateString(state);
89    }
90    
91    public static String JavaDoc getStateString(int stateInt)
92    {
93       return stateNames[stateInt];
94    }
95    
96    public String JavaDoc toString()
97    {
98       StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(512);
99       
100       sbuf.append("ObjectName: ").append(objectName);
101       sbuf.append("\n State: ").append(stateNames[state]);
102
103       if (state == FAILED && problem != null)
104       {
105          sbuf.append("\n Reason: ").append(problem.toString());
106       }
107       printList(sbuf, "\n I Depend On:", iDependOn);
108       printList(sbuf, "\n Depends On Me:", dependsOnMe);
109
110       // this magically makes *all* jmx-console outputs look better :)
111
sbuf.append("\n");
112       
113       return sbuf.toString();
114    }
115    
116    private void printList(StringBuffer JavaDoc sbuf, String JavaDoc msg, List JavaDoc ctxs)
117    {
118       if (ctxs.size() > 0)
119       {
120          // only out stuff, if there are indeed dependencies
121
sbuf.append(msg);
122          for (Iterator JavaDoc i = ctxs.iterator(); i.hasNext(); )
123          {
124             ServiceContext sc = (ServiceContext) i.next();
125             sbuf.append("\n ");
126             sbuf.append(sc.objectName);
127          }
128       }
129    }
130 }
131
Popular Tags