1 | // jademx - JADE management using JMX |
2 | // Copyright 2005-2006 Caboodle Networks, Inc. |
3 | // |
4 | // This library is free software; you can redistribute it and/or |
5 | // modify it under the terms of the GNU Lesser General Public |
6 | // License as published by the Free Software Foundation; either |
7 | // version 2.1 of the License, or (at your option) any later version. |
8 | // |
9 | // This library is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | // Lesser General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU Lesser General Public |
15 | // License along with this library; if not, write to the Free Software |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
17 | |
18 | package jade.jademx.mbean; |
19 | |
20 | import jade.core.Runtime; |
21 | import jade.jademx.config.jademx.onto.JademxConfig; |
22 | import jade.jademx.server.JadeMXServer; |
23 | import jade.jademx.server.JadeMXServerFactory; |
24 | import jade.util.Logger; |
25 | import jade.util.leap.Iterator; |
26 | import jade.util.leap.List; |
27 | |
28 | /** |
29 | * Factory class for <code>jade.management.mbean</code> package. |
30 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
31 | * >Caboodle Networks, Inc.</a> |
32 | */ |
33 | public class JadeFactory { |
34 | |
35 | /** the JadeMXServer we're using */ |
36 | private JadeMXServer jadeMXServer; |
37 | |
38 | /** default ObjectName domain */ |
39 | public final static String OBJECT_NAME_DOMAIN_DEFAULT = "jade"; |
40 | |
41 | /** ObjectName domain for created MBeans*/ |
42 | private String objectNameDomain; |
43 | |
44 | /** default name for thread group */ |
45 | private static final String THREAD_GROUP_DEFAULT_NAME = |
46 | "JADE runtime"; |
47 | |
48 | /** thread group name */ |
49 | private String threadGroupName = THREAD_GROUP_DEFAULT_NAME; |
50 | |
51 | /** default name property for runtime instance */ |
52 | private static final String RUNTIME_NAME_DEFAULT = "default"; |
53 | |
54 | /** name property for JadeRuntime */ |
55 | private String runtimeName = RUNTIME_NAME_DEFAULT; |
56 | |
57 | |
58 | /** my logger */ |
59 | private final Logger logger = |
60 | Logger.getMyLogger(JadeFactory.class.getName()); |
61 | |
62 | |
63 | /** |
64 | * make factory for JADE MBeans for given MBean server. |
65 | * @param jadeMXServer MBean server operating under |
66 | */ |
67 | public JadeFactory( JadeMXServer jadeMXServer ) { |
68 | this( jadeMXServer, |
69 | OBJECT_NAME_DOMAIN_DEFAULT, |
70 | RUNTIME_NAME_DEFAULT, |
71 | THREAD_GROUP_DEFAULT_NAME ); |
72 | } |
73 | |
74 | |
75 | /** |
76 | * make factory for JADE MBeans for given MBean server. |
77 | * @param jadeMXServer MBean server operating under |
78 | * @param objectNameDomain ObjectName domain to use |
79 | */ |
80 | public JadeFactory( JadeMXServer jadeMXServer, String objectNameDomain ) { |
81 | this( jadeMXServer, objectNameDomain, RUNTIME_NAME_DEFAULT, |
82 | THREAD_GROUP_DEFAULT_NAME ); |
83 | } |
84 | |
85 | |
86 | /** |
87 | * make factory for JADE MBeans for given MBean server. |
88 | * @param jadeMXServer MBean server operating under |
89 | * @param objectNameDomain ObjectName domain to use |
90 | * @param runtimeName name property to use for JadeRuntimeMBean |
91 | */ |
92 | public JadeFactory( JadeMXServer jadeMXServer, String objectNameDomain, |
93 | String runtimeName ) { |
94 | this( jadeMXServer, objectNameDomain, runtimeName, |
95 | THREAD_GROUP_DEFAULT_NAME ); |
96 | } |
97 | |
98 | |
99 | /** |
100 | * make factory for JADE MBeans for given MBean server. |
101 | * @param jadeMXServer MBean server operating under |
102 | * @param objectNameDomain ObjectName domain to use |
103 | * @param runtimeName name property to use for JadeRuntimeMBean |
104 | * @param threadGroupName name for thread group for JADE platforms |
105 | */ |
106 | public JadeFactory( |
107 | JadeMXServer jadeMXServer, |
108 | String objectNameDomain, |
109 | String runtimeName, |
110 | String threadGroupName ) { |
111 | this.jadeMXServer = jadeMXServer; |
112 | this.objectNameDomain = objectNameDomain; |
113 | this.runtimeName = runtimeName; |
114 | this.threadGroupName = threadGroupName; |
115 | logger.log( Logger.CONFIG, "created jademx JadeFactory for domain \"" + |
116 | objectNameDomain + "\", thread group \""+threadGroupName+"\""); |
117 | } |
118 | |
119 | |
120 | /** |
121 | * Factory class wrapper for <code>jade.core.Runtime.instance()</code>. |
122 | * Callers should call <code>unregister()</code> after all done with |
123 | * the instance. |
124 | * @return MBean for singleton JADE runtime |
125 | */ |
126 | public JadeRuntimeMBean runtimeInstance() |
127 | throws JademxException { |
128 | JadeMXServerFactory.setSysProp( jadeMXServer ); |
129 | // FIXME: inconsistency between new JadeRuntime MBean and single |
130 | // JADE Runtime instance |
131 | JadeRuntime jadeRuntime = new JadeRuntime( this, Runtime.instance() ); |
132 | try { |
133 | jadeRuntime.register( runtimeName, null ); |
134 | } |
135 | catch ( Exception e ) { |
136 | throw new JademxException("exception registering "+runtimeName, e ); |
137 | } |
138 | logger.log( Logger.CONFIG, "created JadeRuntimeMBean instance \"" + |
139 | jadeRuntime.getObjectName().getCanonicalName() + "\""); |
140 | return jadeRuntime; |
141 | } |
142 | |
143 | /** |
144 | * instantiate the runtime specified by jademx configuration object |
145 | * @param jademxConfig jademx configuration to instantiate |
146 | * @return instantiated runtime mbean |
147 | * @throws JademxException on error |
148 | */ |
149 | public JadeRuntimeMBean instantiateRuntime( JademxConfig jademxConfig ) |
150 | throws JademxException { |
151 | JadeRuntime jadeRuntime = (JadeRuntime)runtimeInstance(); |
152 | List runtimes = jademxConfig.getRuntimes(); |
153 | if ( null != runtimes ) { |
154 | Iterator runtimeI = runtimes.iterator(); |
155 | if ( runtimeI.hasNext() ) { |
156 | jade.jademx.config.jademx.onto.ConfigRuntime runtimeConfig = |
157 | (jade.jademx.config.jademx.onto.ConfigRuntime)runtimeI.next(); |
158 | jadeRuntime.instantiatePlatforms( runtimeConfig ); |
159 | } |
160 | } |
161 | return jadeRuntime; |
162 | } |
163 | |
164 | /** |
165 | * return the object name domain |
166 | * @return returns the object name domain |
167 | */ |
168 | public String getObjectNameDomain() { |
169 | return objectNameDomain; |
170 | } |
171 | |
172 | /** |
173 | * return the MBean server |
174 | * @return returns the MBean server. |
175 | */ |
176 | public JadeMXServer getJadeMXServer() { |
177 | return jadeMXServer; |
178 | } |
179 | |
180 | /** |
181 | * return the thread group name |
182 | * @return the thread group name |
183 | */ |
184 | public String getThreadGroupName() { |
185 | return threadGroupName; |
186 | } |
187 | /** |
188 | * get runtime name |
189 | * @return Returns the runtimeName. |
190 | */ |
191 | public String getRuntimeName() { |
192 | return runtimeName; |
193 | } |
194 | |
195 | /* (non-Javadoc) |
196 | * @see java.lang.Object#toString() |
197 | */ |
198 | public String toString() { |
199 | return JadeFactory.class.getName() + |
200 | "[objectNameDomain="+objectNameDomain+ |
201 | ",threadGroupName="+threadGroupName+ |
202 | ",runtimeName="+runtimeName+"]"; |
203 | } |
204 | } |