KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > j2ee > DASJ2EEServerImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.j2ee;
24
25 import java.util.Set JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.management.ObjectName JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MBeanInfo JavaDoc;
33 import javax.management.MBeanAttributeInfo JavaDoc;
34 import javax.management.MBeanOperationInfo JavaDoc;
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.MBeanException JavaDoc;
37 import javax.management.AttributeNotFoundException JavaDoc;
38 import javax.management.InstanceNotFoundException JavaDoc;
39 import javax.management.ReflectionException JavaDoc;
40 import javax.management.MBeanServerInvocationHandler JavaDoc;
41
42 import com.sun.appserv.management.base.Util;
43 import com.sun.appserv.management.util.misc.GSetUtil;
44 import com.sun.appserv.management.util.jmx.JMXUtil;
45 import com.sun.appserv.management.util.misc.ExceptionUtil;
46 import com.sun.appserv.management.client.ProxyFactory;
47 import com.sun.appserv.management.j2ee.J2EEServer;
48 import com.sun.appserv.management.j2ee.J2EETypes;
49 import com.sun.appserv.management.j2ee.StateManageable;
50 import com.sun.appserv.management.j2ee.J2EEServer;
51
52 import com.sun.enterprise.management.support.Delegate;
53 import com.sun.enterprise.management.support.DummyDelegate;
54 import com.sun.enterprise.management.support.LoaderMBean;
55 import com.sun.enterprise.management.support.QueryMgrImpl;
56 import com.sun.enterprise.management.support.DelegateToMBeanDelegate;
57
58 import com.sun.enterprise.admin.mbeans.DomainStatusMBean;
59 import com.sun.enterprise.admin.mbeans.DomainStatus;
60 import com.sun.enterprise.admin.mbeans.DomainStatusHelper;
61 import com.sun.enterprise.ManagementObjectManager;
62
63 import com.sun.appserv.management.j2ee.J2EETypes;
64
65 /**
66     JSR 77 extension representing an Appserver standalone server (non-clustered)
67
68     Server MBean which will reside on DAS
69     for enabling state management including start() and stop()
70  */

71 public class DASJ2EEServerImpl
72     extends J2EEServerImpl
73     implements NotificationListener JavaDoc
74 {
75     // vars
76
private Delegate delegate;
77
78         public
79     DASJ2EEServerImpl()
80     {
81         super( J2EETypes.J2EE_SERVER, DummyDelegate.INSTANCE );
82         delegate = DummyDelegate.INSTANCE;
83     }
84
85     static private final Class JavaDoc[] DOMAIN_STATUS_INTERFACES =
86         new Class JavaDoc[] { DomainStatusMBean.class };
87     
88         protected DomainStatusMBean
89     getDomainStatus()
90     {
91         DomainStatusMBean domainStatus = null;
92         try {
93             final MBeanServer JavaDoc mbeanServer = getMBeanServer();
94             final Set JavaDoc<ObjectName JavaDoc> candidates = QueryMgrImpl.queryPatternObjectNameSet(
95                 mbeanServer, JMXUtil.newObjectNamePattern(
96                     "*", DomainStatusMBean.DOMAIN_STATUS_PROPS ) );
97             final ObjectName JavaDoc on = GSetUtil.getSingleton( candidates );
98             domainStatus = (DomainStatusMBean)MBeanServerInvocationHandler.
99                 newProxyInstance( mbeanServer, on, DomainStatusMBean.class, false );
100         } catch (Exception JavaDoc e) {
101             final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
102             getMBeanLogger().warning( rootCause.toString() + "\n" +
103                 ExceptionUtil.getStackTrace( rootCause ) );
104         }
105         return( domainStatus );
106     }
107     
108         private boolean
109     remoteServerIsRunning()
110     {
111         return (StateManageable.STATE_RUNNING == getstate());
112     }
113
114         private boolean
115     remoteServerIsStartable()
116     {
117         int cState = getstate();
118
119         if ((StateManageable.STATE_STOPPED == cState) ||
120             (StateManageable.STATE_FAILED == cState))
121         {
122             return true;
123         }
124         else
125         {
126             return false;
127         }
128     }
129     
130         private boolean
131     remoteServerIsStoppable()
132     {
133         int cState = getstate();
134
135         if ((StateManageable.STATE_STARTING == cState) ||
136             (StateManageable.STATE_RUNNING == cState) ||
137             (StateManageable.STATE_FAILED == cState))
138         {
139             return true;
140         }
141         else
142         {
143             return false;
144         }
145     }
146     
147         public void
148     handleNotification( Notification JavaDoc notif , Object JavaDoc obj)
149     {
150         final String JavaDoc notifType = notif.getType();
151         
152         if ( notifType.equals( DomainStatusMBean.SERVER_STATUS_NOTIFICATION_TYPE ) )
153         {
154             final String JavaDoc serverName = (String JavaDoc)
155                 Util.getAMXNotificationValue( notif, DomainStatusMBean.SERVER_NAME_KEY );
156             
157             if ( serverName.equals( getServerName() ) )
158             {
159                 setDelegate();
160             }
161         }
162     }
163     
164         private void
165     setDelegate()
166     {
167         if ( remoteServerIsRunning() )
168         {
169             try {
170             // get the object name for the old jsr77 server mBean
171
com.sun.enterprise.ManagementObjectManager mgmtObjManager =
172                 com.sun.enterprise.Switch.getSwitch().getManagementObjectManager();
173
174             String JavaDoc strON = mgmtObjManager.getServerBaseON(false, getServerName());
175             
176             
177             final MBeanServerConnection JavaDoc remoteConn =
178                 getDomainStatus().getServerMBeanServerConnection( getServerName() );
179                 
180             ObjectName JavaDoc onPattern = new ObjectName JavaDoc(strON + ",*");
181
182             final Set JavaDoc<ObjectName JavaDoc> names = JMXUtil.queryNames( remoteConn, onPattern, null);
183
184             assert( names.size() == 1 );
185
186             final ObjectName JavaDoc serverON = GSetUtil.getSingleton( names );
187
188             final Delegate delegate =
189                 new DelegateToMBeanDelegate( remoteConn, serverON );
190             
191             setDelegate( delegate );
192             setstartTime(System.currentTimeMillis());
193             } catch (Exception JavaDoc e) {
194                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
195                         getMBeanLogger().warning( rootCause.toString() + "\n" +
196                                         ExceptionUtil.getStackTrace( rootCause ) );
197                     }
198         }
199         else
200         {
201             setDelegate( DummyDelegate.INSTANCE );
202             setstartTime(0);
203         }
204     }
205     
206         public void
207     preRegisterDone()
208         throws Exception JavaDoc
209     {
210         super.preRegisterDone( );
211         
212         setstartTime( 0 );
213         setDelegate();
214     }
215     
216         protected String JavaDoc
217     getServerName()
218     {
219         return( getSelfName() );
220     }
221
222         public boolean
223     isstateManageable()
224     {
225         return true;
226     }
227
228         public int
229     getstate()
230     {
231         try {
232             return (getDomainStatus().getstate(getServerName()));
233         } catch (Exception JavaDoc e) {
234                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( e );
235                         getMBeanLogger().warning( rootCause.toString() + "\n" +
236                                         ExceptionUtil.getStackTrace( rootCause ) );
237                 }
238         return StateManageable.STATE_FAILED;
239     }
240
241         public void
242     start()
243     {
244         if ( remoteServerIsStartable() )
245         {
246             startRemoteServer();
247         }
248         else
249         {
250             throw new RuntimeException JavaDoc("server is not in a startable state");
251         }
252     }
253     
254         public void
255     startRecursive()
256     {
257         start();
258     }
259     
260         public void
261     stop()
262     {
263         if ( remoteServerIsStoppable() )
264         {
265             stopRemoteServer();
266         }
267         else
268         {
269             throw new RuntimeException JavaDoc("server is not in a stoppable state");
270         }
271     }
272
273
274         public MBeanInfo JavaDoc
275     getMBeanInfo()
276     {
277
278         MBeanInfo JavaDoc superMBeanInfo = super.getMBeanInfo();
279
280         if (delegate != null)
281         {
282             try {
283                 DummyDelegate dd = (DummyDelegate) delegate;
284                 final MBeanInfo JavaDoc mbeanInfo = new MBeanInfo JavaDoc(
285                     superMBeanInfo.getClassName(),
286                     superMBeanInfo.getDescription(),
287                     mergeAttributeInfos(superMBeanInfo.getAttributes(),
288                         getMBeanAttributeInfo()),
289                     superMBeanInfo.getConstructors(),
290                     mergeOperationInfos(superMBeanInfo.getOperations(),
291                         getMBeanOperationInfo()),
292                     superMBeanInfo.getNotifications());
293                 return( mbeanInfo );
294             } catch (ClassCastException JavaDoc cce) {
295                 return superMBeanInfo;
296             }
297         }
298         else
299         {
300             return superMBeanInfo;
301         
302         }
303
304     }
305
306
307     // attribute info
308

309         private MBeanAttributeInfo JavaDoc[]
310     mergeAttributeInfos(
311         MBeanAttributeInfo JavaDoc[] infos1,
312         MBeanAttributeInfo JavaDoc[] infos2 )
313     {
314         final MBeanAttributeInfo JavaDoc[] infos =
315             new MBeanAttributeInfo JavaDoc[ infos1.length + infos2.length ];
316         
317         System.arraycopy( infos1, 0, infos, 0, infos1.length );
318         System.arraycopy( infos2, 0, infos, infos1.length, infos2.length );
319         
320         return( infos );
321     }
322
323
324         private MBeanAttributeInfo JavaDoc[]
325     getMBeanAttributeInfo()
326     {
327         MBeanAttributeInfo JavaDoc[] dAttributes = new MBeanAttributeInfo JavaDoc[1];
328         dAttributes[0] = new MBeanAttributeInfo JavaDoc("state",
329                                                 "java.lang.Integer",
330                                                 "server state",
331                                                 true,
332                                                 false,
333                                                 false);
334         return dAttributes;
335     }
336
337
338     // operation info
339

340         private MBeanOperationInfo JavaDoc[]
341     mergeOperationInfos(
342         MBeanOperationInfo JavaDoc[] infos1,
343         MBeanOperationInfo JavaDoc[] infos2 )
344     {
345         final MBeanOperationInfo JavaDoc[] infos =
346             new MBeanOperationInfo JavaDoc[ infos1.length + infos2.length ];
347         
348         System.arraycopy( infos1, 0, infos, 0, infos1.length );
349         System.arraycopy( infos2, 0, infos, infos1.length, infos2.length );
350         
351         return( infos );
352     }
353
354
355         private MBeanOperationInfo JavaDoc[]
356     getMBeanOperationInfo()
357     {
358         MBeanOperationInfo JavaDoc[] dOperations = new MBeanOperationInfo JavaDoc[3];
359         dOperations[0] = new MBeanOperationInfo JavaDoc("start",
360                                                 "start server instance",
361                                                 null,
362                                                 "void",
363                                                 MBeanOperationInfo.ACTION);
364         dOperations[1] = new MBeanOperationInfo JavaDoc("stop",
365                                                 "stop server instance",
366                                                 null,
367                                                 "void",
368                                                 MBeanOperationInfo.ACTION);
369         dOperations[2] = new MBeanOperationInfo JavaDoc("startRecursive",
370                                                 "start server instance",
371                                                 null,
372                                                 "void",
373                                                 MBeanOperationInfo.ACTION);
374         return dOperations;
375     }
376
377
378         private void
379     startRemoteServer()
380     {
381
382         try {
383             // get the object name for servers config mBean
384
ObjectName JavaDoc on = DomainStatusHelper.getServersConfigObjectName();
385
386             // invoke start method on servers config mBean
387

388             // get mBean server
389
final MBeanServer JavaDoc server = getMBeanServer();
390
391             // form the parameters
392
Object JavaDoc[] params = new Object JavaDoc[1];
393                     params[0] = getServerName();
394                     String JavaDoc[] signature = {"java.lang.String"};
395
396             // invoke the start method
397
server.invoke(on, "startServerInstance", params, signature);
398         } catch (javax.management.MalformedObjectNameException JavaDoc mfone) {
399                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( mfone );
400                         getMBeanLogger().warning( rootCause.toString() + "\n" +
401                                         ExceptionUtil.getStackTrace( rootCause ) );
402             throw new RuntimeException JavaDoc(mfone);
403         } catch (javax.management.InstanceNotFoundException JavaDoc infe) {
404                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( infe );
405                         getMBeanLogger().warning( rootCause.toString() + "\n" +
406                                         ExceptionUtil.getStackTrace( rootCause ) );
407             throw new RuntimeException JavaDoc(infe);
408         } catch (javax.management.MBeanException JavaDoc mbe) {
409                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( mbe );
410                         getMBeanLogger().warning( rootCause.toString() + "\n" +
411                                         ExceptionUtil.getStackTrace( rootCause ) );
412             throw new RuntimeException JavaDoc(mbe);
413         } catch (javax.management.ReflectionException JavaDoc rfe) {
414                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( rfe );
415                         getMBeanLogger().warning( rootCause.toString() + "\n" +
416                                         ExceptionUtil.getStackTrace( rootCause ) );
417             throw new RuntimeException JavaDoc(rfe);
418         }
419     }
420
421
422         private void
423     stopRemoteServer()
424     {
425
426         try {
427             // get the object name for servers config mBean
428
ObjectName JavaDoc on = DomainStatusHelper.getServersConfigObjectName();
429
430             // invoke stop method on servers config mBean
431

432             // get mBean server
433
final MBeanServer JavaDoc server = getMBeanServer();
434
435             // form the parameters
436
Object JavaDoc[] params = new Object JavaDoc[1];
437                     params[0] = getServerName();
438                     String JavaDoc[] signature = {"java.lang.String"};
439
440             // invoke the start method
441
server.invoke(on, "stopServerInstance", params, signature);
442         } catch (javax.management.MalformedObjectNameException JavaDoc mfone) {
443                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( mfone );
444                         getMBeanLogger().warning( rootCause.toString() + "\n" +
445                                         ExceptionUtil.getStackTrace( rootCause ) );
446             throw new RuntimeException JavaDoc(mfone);
447         } catch (javax.management.InstanceNotFoundException JavaDoc infe) {
448             // in case of PE and DAS
449
// it is desit]rable that the instance be not stopped
450
// hence the log level is fine
451
final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( infe );
452                         getMBeanLogger().fine( rootCause.toString() + "\n" +
453                                         ExceptionUtil.getStackTrace( rootCause ) );
454             throw new RuntimeException JavaDoc(infe);
455         } catch (javax.management.MBeanException JavaDoc mbe) {
456                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( mbe );
457                         getMBeanLogger().warning( rootCause.toString() + "\n" +
458                                         ExceptionUtil.getStackTrace( rootCause ) );
459             throw new RuntimeException JavaDoc(mbe);
460         } catch (javax.management.ReflectionException JavaDoc rfe) {
461                         final Throwable JavaDoc rootCause = ExceptionUtil.getRootCause( rfe );
462                         getMBeanLogger().warning( rootCause.toString() + "\n" +
463                                         ExceptionUtil.getStackTrace( rootCause ) );
464             throw new RuntimeException JavaDoc(rfe);
465         }
466     }
467
468 }
469
Popular Tags