KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > AdminService


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
24 package com.sun.enterprise.admin;
25
26 //JDK imports
27
import java.io.File JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.URL JavaDoc;
30
31 //JMX imports
32
import javax.management.MBeanServer JavaDoc;
33 import javax.management.ObjectInstance JavaDoc;
34
35
36 /*
37 import com.iplanet.ias.admin.event.AdminEventListenerRegistry;
38 import com.iplanet.ias.admin.event.WebConfigChangeEventListener;
39 import com.iplanet.ias.admin.monitor.GenericMonitorMBean;
40 import com.iplanet.ias.admin.monitor.MonitoringChannel;
41 import com.iplanet.ias.admin.server.core.channel.AdminChannel;
42 import com.iplanet.ias.instance.ServerManager;
43 import com.iplanet.ias.admin.common.constant.AdminConstants;
44 import com.iplanet.ias.instance.InstanceEnvironment;
45 import com.iplanet.ias.server.Constants;
46
47 import com.iplanet.ias.server.ServerContext;
48  */

49 import com.sun.appserv.server.ServerLifecycleException;
50
51 /*
52 import com.iplanet.ias.admin.common.domains.registry.DomainRegistry;
53 import com.iplanet.ias.admin.common.domains.registry.DomainEntry;
54 import com.iplanet.ias.admin.common.domains.registry.ContactDataSet;
55 import com.iplanet.ias.admin.common.domains.registry.ContactData;
56 import com.iplanet.ias.admin.common.domains.registry.DomainRegistryException;
57 import com.iplanet.ias.config.ConfigException;
58 import com.iplanet.ias.config.serverbeans.Server;
59 import com.iplanet.ias.config.serverbeans.HttpService;
60 import com.iplanet.ias.config.serverbeans.HttpListener;
61  */

62
63 import com.sun.enterprise.config.ConfigFactory;
64
65 import java.io.File JavaDoc;
66 import javax.management.MBeanServerFactory JavaDoc;
67 import javax.management.MBeanServer JavaDoc;
68 import javax.management.MBeanServerBuilder JavaDoc;
69
70 /*
71 //Logging related imports
72 import java.util.logging.Level;
73 import java.util.logging.Logger;
74 import com.sun.logging.LogDomains;
75
76 //Utility methods
77 import com.iplanet.ias.util.io.FileUtils;
78
79  */

80 import com.sun.enterprise.admin.meta.MBeanRegistry;
81 import com.sun.enterprise.admin.config.ConfigMBeansManager;
82 import com.sun.enterprise.management.ManagementObjectManager;
83 import com.sun.enterprise.management.util.J2EEManagementObjectManager;
84
85
86 /**
87  * Admin service is run every instance and acts as gateway to server
88  * administration tasks.
89  *
90  * NEW: Admin service determines if the running instance is
91  * DAS or DASD or INSTANCE. Based on that it figures out
92  * what to load.
93  */

94 public class AdminService {
95
96     /*
97      * Flag to enable performance
98      */

99     public static final boolean ENABLE_PERFORMANCE_THREAD = true;
100     
101     /** The logger from LogDomains */
102     /* Note that this is the ONLY class that is dependent on the LogDomains.
103      * Once registered through LogDomains, others can get it using standard
104      * call Logger.getLogger() */

105     
106     /*
107     //FIXME: removed for now.
108     public static final Logger sLogger
109             = LogDomains.getLogger(AdminConstants.kLoggerName);
110      */

111     private static AdminService adminService = null;
112     private static String JavaDoc adminType = null;
113     private static final String JavaDoc DASD_TYPE = "DASD";
114     private static final String JavaDoc DAS_TYPE = "DAS";
115     private static final String JavaDoc INSTANCE_TYPE = "INSTANCE";
116
117     private static String JavaDoc kTempDirNamePrefix = "s1astemp";
118     private static String JavaDoc kGUITempDirName = "gui";
119     /*
120     private static String kTempDirNameSuffix =
121             "" + ((System.getProperty(Constants.IAS_ROOT) != null) ?
122             System.getProperty(Constants.IAS_ROOT).hashCode() : 0);
123      */

124     
125     /* The temporary directory name should be a function of domain name,
126      * admin-server id and the install root, otherwise it won't
127      * work for the multiple installations on the same machine. */

128     
129     private static int sMBeanServerID;
130     public static final int kDefaultImpl = 0;
131     
132     //private ServerContext context;
133

134     private String JavaDoc mTempDirPath;
135     private String JavaDoc mGUITempDirPath;
136
137     /**
138      * Package constructor. AdminService life cycle is managed through
139      * LifeCycleManager object in this package.
140      */

141     AdminService() {
142         sMBeanServerID = kDefaultImpl; //the only implementation for now
143
}
144
145     /**
146      * set AdminService. This method should be called by LifeCycleManager
147      * after instantiating the service. This enables other objects to use
148      * static method getAdminService to get access to admin service.
149      */

150     static void setAdminService(AdminService srv) {
151         adminService = srv;
152     }
153
154     /**
155      * Get admin service object. Admin service is started when server starts
156      * up.
157      */

158     public static AdminService getAdminService() {
159         return adminService;
160     }
161
162     /**
163      * Initialize admin service. This is called just after creating the
164      * instance and before any public methods are called on admin service.
165      * Initializes the admin server's MBeanServer.
166      * Currently initializes the MBeanServer with default implementation in
167      * com.iplanet.ias.admin.server.core.jmx.MBeanServerImpl.
168      * Should there be multiple implementations, this method needs to be
169      * modified.
170      * @throws LifeCycleException in case the initialzation fails.
171      */

172     void init() throws ServerLifecycleException {
173         /*
174         MBeanServer mbs = null;
175
176         
177         if (sMBeanServerID == kDefaultImpl) {
178             try {
179                 mbs = ASMBeanServerImpl.getMBeanServerInstance();
180                 MBeanServerFactory.initialize(this, mbs);
181                 if (isAdminInstance()) {
182                     sLogger.log(Level.INFO, "core.mbs_init_ok");
183                     createTimeStampFilesForInstances();
184                     //background check for manualchanges
185                     if(ENABLE_PERFORMANCE_THREAD) ManualChangeTracker.start();
186                 }
187                 else {
188                     if (sLogger.isLoggable(Level.FINEST)) {
189                         sLogger.log(Level.FINEST,"core.mbs_init_ok");
190                     }
191                 }
192                 GenericMonitorMBean root = GenericMonitorMBean.getRoot();
193                 ObjectInstance oi = mbs.registerMBean(root, root.getObjectName());
194                 String oName = oi.getObjectName().toString();
195                 if (sLogger.isLoggable(Level.FINEST)) {
196                     sLogger.log(Level.FINEST, "monitor.reg_root_mbean", oName);
197                 }
198             }
199             catch (Throwable t) {
200                 if (isAdminInstance()) {
201                     sLogger.log(Level.SEVERE, "core.mbs_init_exception", t);
202                 }
203                 else {
204                     sLogger.log(Level.WARNING, "core.mbs_init_exception", t);
205                 }
206                 throw new ServerLifecycleException(t.getMessage());
207             }
208         }
209          */

210         
211         // These are common for all admin types. needs to happen on init.
212

213         createConfigContext();
214         createRegistry();
215         createMBS();
216         createAdminServiceMBean();
217     }
218     
219     
220     /**
221      * Start admin service. This is called prior to any public method call
222      * on admin service. This should be used to prepare admin service to
223      * receive public method calls.
224      */

225     void start() throws ServerLifecycleException {
226         if (isAdminInstance()) {
227             startAdminInstance();
228         } else {
229             startNormalInstance();
230         }
231     }
232
233     /**
234      * Admin service is ready. All other services have started up successfully.
235      */

236     void ready() throws ServerLifecycleException {
237         if (isAdminInstance()) {
238             readyAdminInstance();
239         } else {
240             readyNormalInstance();
241         }
242     }
243
244     /**
245      * Stop admin service. This is called when shutdown process starts.
246      */

247     void stop() throws ServerLifecycleException {
248         if (isAdminInstance()) {
249             //stop checking for manual changes
250
//if(ENABLE_PERFORMANCE_THREAD) ManualChangeTracker.stop();
251
stopAdminInstance();
252         } else {
253             stopNormalInstance();
254         }
255     }
256
257     /**
258      * Destroy admin service. This is called just before JVM is destroyed.
259      */

260     void destroy() throws ServerLifecycleException {
261     }
262
263     /**
264      * Get server context.
265      * /
266     public ServerContext getContext() {
267         return ((adminService == null) ? null : adminService.context);
268     }
269
270     /**
271      * Set context to specified value. Typically, lifecycle manager will call
272      * this method during initialization to set appropriate context.
273      * /
274     void setContext(ServerContext ctx) {
275         context = ctx;
276     }
277
278     /**
279      * Get name of server instance.
280      */

281     public String JavaDoc getInstanceName() {
282         return "PE";
283         //FIXME: NYI
284
//return ((context == null) ? null : context.getInstanceName());
285
}
286
287     /**
288      * Get root monitoring MBean. Monitoring MBeans within a server instance
289      * are organized in a tree. This object represents the root of the tree.
290      * /
291     public GenericMonitorMBean getRootMonitorMBean() {
292         return GenericMonitorMBean.getRoot();
293     }
294
295     /**
296      * Helper method to start admin server instance
297      */

298     private void startAdminInstance() throws ServerLifecycleException {
299         //FIXME
300

301         //setAdminInstanceProperties();
302

303         //NEW: now we know we are either DASD or DAS
304

305         // start some mbeans. no lazy loading for these mbeans
306
registerAdminMBeans();
307         
308         if(DASD_TYPE.equals(adminType)) {
309             //enable deployment. how?
310
}
311         
312         //AdminChannel.createRMIChannel();
313
//AdminChannel.createSharedSecret();
314

315         // Test code here
316
// new com.iplanet.ias.admin.event.EventTester();
317
// Uncomment following line to test monitoring
318
// new com.iplanet.ias.admin.monitor.MonitoringTester();
319

320         //FIXME NYI
321
//createTempDir();
322
}
323
324     /**
325      * Helper method to perform necessary tasks when admin service is set as
326      * ready.
327      */

328     private void readyAdminInstance() throws ServerLifecycleException {
329         /*
330         AdminChannel.setRMIChannelReady();
331         updateDomainRegistry();
332          */

333     }
334
335     /**
336      * Helper method to stop admin server instance.
337      */

338     private void stopAdminInstance() throws ServerLifecycleException {
339         /*
340         deleteTempDir();
341         AdminChannel.destroyRMIChannel();
342          */

343     }
344
345     /**
346      * Helper method to start normal (non-admin) instances
347      */

348     private void startNormalInstance() throws ServerLifecycleException {
349         //AdminChannel.createRMIChannel();
350
//AdminChannel.createSharedSecret();
351
//AdminEventListenerRegistry.addMonitoringEventListener(
352
// new MonitoringChannel());
353
//WebConfigChangeEventListener web = new WebConfigChangeEventListener();
354
//AdminEventListenerRegistry.addConfigChangeEventListener(
355
// web.getConfigChangeCategory(), web);
356
// Test code here
357
// new com.iplanet.ias.admin.event.EventTester("str");
358
}
359
360     /**
361      * Helper method to set normal instances to ready state.
362      */

363     private void readyNormalInstance() throws ServerLifecycleException {
364         //AdminChannel.setRMIChannelReady();
365
}
366
367     /**
368      * Helper method to stop normal (non-admin) instances
369      */

370     private void stopNormalInstance() throws ServerLifecycleException {
371         //AdminChannel.destroyRMIChannel();
372
}
373
374     /**
375      * Is this admin service running within admin server instance. Admin service
376      * runs in all server instances.
377      * @return true if the service is running in an admin server instance,
378      * false otherwise.
379      */

380     //FIXME: NYI
381
public boolean isAdminInstance() {
382         return true;
383         /*
384         if (ServerManager.ADMINSERVER_ID.equals(context.getInstanceName())) {
385             return true;
386         } else {
387             return false;
388         }
389          */

390     }
391     
392     /**
393      Creates the time stamp files (if do not exist) for the instances that the
394      admin server manages.
395     * /
396     private void createTimeStampFilesForInstances() {
397         String[] instanceIds = ServerManager.instance().getInstanceNames(true);
398         for (int i = 0 ; i < instanceIds.length ; i ++) {
399             try {
400                 String instanceId = instanceIds[i];
401                 InstanceEnvironment ie = new InstanceEnvironment(instanceId);
402                 ie.createTimeStampFiles();
403                 sLogger.log(Level.INFO, "core.ts_files_ok", instanceId);
404             }
405             catch (Exception e) {
406                 //Log the exception for this instance, squelching is OK?
407                 sLogger.log(Level.WARNING, "core.ts_files_failed", e);
408             }
409         }
410         
411     }
412
413     /**
414         Gets the name of the temporary folder where the admin-server would be
415         creating some temporary data. Note that this method is not a pure
416         accessor in the sense that it will create the folder on disk if
417         it does not exist.
418         @return String representing the absolute path of temporary folder, which
419         may be null if the file creation fails.
420     */

421     public String JavaDoc getTempDirPath() {
422         if (mTempDirPath == null) {
423         /* Give it one more try, if on the startup the
424            user does not have enough space and/or the temp
425            file was not created. Note that the first-time failure will
426            be detected in the log at startup.
427            Also note that this additional safety does not come without
428            the overhead of the null check for every call to this function.
429        */

430            // createTempDir();
431
}
432         
433         return ( mTempDirPath );
434     }
435
436     /**
437         Gets the name of the temporary folder where the admin-GUI would be
438         creating some temporary data. Note that this method is not a pure
439         accessor in the sense that it will create the folder on disk if
440         it does not exist.
441         @return String representing the absolute path of temporary folder
442         for GUI, which may be null if the file creation fails.
443     */

444     public String JavaDoc getGUITempDirPath() {
445         if (mGUITempDirPath == null) {
446         /* Give it one more try, if on the startup the
447            user does not have enough space and/or the temp
448            file was not created. Note that the first-time failure will
449            be detected in the log at startup.
450            Also note that this additional safety does not come without
451            the overhead of the null check for every call to this function.
452        */

453            // createTempDir();
454
}
455         
456         return ( mGUITempDirPath );
457     }
458
459     /*
460     private void createTempDir() {
461         try {
462             String domainName = ServerManager.instance().getDomainName();
463             String localTmpDir = System.getProperty("java.io.tmpdir");
464             String asTempDirName = kTempDirNamePrefix + domainName +
465                     ServerManager.ADMINSERVER_ID + kTempDirNameSuffix;
466      */

467             /* for admin server, e.g. </tmp>/s1astempdomain1admin-server */
468     /*
469             File tempFolder = new File(localTmpDir, asTempDirName);
470             mTempDirPath = tempFolder.getCanonicalPath();
471
472      */

473             /* for GUI, e.g. </tmp>/s1astempdomain1admin-server/gui*/
474     /*
475             File guiTempFolder = new File(mTempDirPath, kGUITempDirName);
476             mGUITempDirPath = guiTempFolder.getCanonicalPath();
477             if (tempFolder.exists()) {
478      */

479                 /*
480                     If it exists, the delete method during earlier admin-server
481                     lifecycle was not able to delete the directory and
482                     that should be OK. The intent here is to create a
483                     directory if there does not exist one.
484                 */

485     /*
486                 sLogger.log(Level.FINEST, "core.tmp_folder_exists",
487                     mTempDirPath);
488                 return;
489             }
490      */

491             /* try to create admin-server's temporary directory */
492     /*
493             boolean couldCreate = tempFolder.mkdirs();
494             if (! couldCreate) {
495                 sLogger.log(Level.WARNING, "core.tmp_folder_creation_failed",
496                         mTempDirPath);
497             }
498             else {
499                 sLogger.log(Level.FINEST, "core.tmp_folder_created_ok",
500                     mTempDirPath);
501             }
502      */

503             /* try to create temporary directory for GUI*/
504     /*
505             couldCreate = guiTempFolder.mkdirs();
506             if (! couldCreate) {
507                 sLogger.log(Level.WARNING, "core.gui_tmp_folder_creation_failed",
508                         mGUITempDirPath);
509             }
510             else {
511                 sLogger.log(Level.FINEST, "core.gui_tmp_folder_created_ok",
512                     mGUITempDirPath);
513             }
514         }
515         catch(Throwable t) {
516             sLogger.log(Level.WARNING, "core.tmp_folder_creation_failed", t);
517         }
518     }
519
520     private void deleteTempDir() {
521         try {
522             FileUtils.whack(new File(mTempDirPath));
523             sLogger.log(Level.FINEST, "core.tmp_folder_deleted_ok",
524                 mTempDirPath);
525         }
526         catch(Throwable t) {
527             sLogger.log(Level.WARNING, "core.tmp_folder_deletion_failed",
528                     mTempDirPath);
529         }
530     }
531
532      */

533     
534     /**
535      * Updates the Domain Registry with the Admin Server's listener
536      * information
537      * /
538     private void updateDomainRegistry() {
539         try{
540             //get the DomainName of this admin server
541             String domainName = ServerManager.instance().getDomainName();
542             //get the DomainRegistryInstance
543             DomainRegistry dreg = DomainRegistry.newInstance();
544             //get the current Domain's entry in the registry
545             DomainEntry de = dreg.getDomain(domainName);
546             //get the DomainRoot
547             File domainRoot = de.getRoot();
548             //create a new ContactDataSet Object to hold
549             //updated ContactData
550             ContactDataSet cds = new ContactDataSet();
551
552             //create the ContactDataSet Object
553             Server s = (Server)context.getConfigBean();
554             HttpListener[] listeners = (s.getHttpService()).getHttpListener();
555             for(int i=0;i< listeners.length;i++){
556               HttpListener listener = listeners[i];
557               cds.add(new ContactData(listener.getServerName(),listener.getPort(),listener.isSecurityEnabled()));
558             }
559             //create the DomainEntry object for update
560             DomainEntry domainEntry = new DomainEntry(domainName,domainRoot, cds);
561
562             //Call Domain Registry.reregister
563                dreg.reregisterDomain(domainEntry);
564         }
565         catch(DomainRegistryException dre){
566             sLogger.log(Level.WARNING, "core.domain_reregistration_failed");
567             sLogger.log(Level.FINE, "core.domain_exception_occured", dre);
568         }
569         catch(ConfigException ce){
570             sLogger.log(Level.WARNING, "core.domain_reregistration_failed");
571             sLogger.log(Level.FINE, "core.config_exception_occured", ce);
572         }
573         catch (Throwable t) {
574             sLogger.log(Level.WARNING, "core.domain_reregistration_failed");
575             sLogger.log(Level.FINE, "general.unexpected_exception", t);
576         }
577     }
578
579     /**
580      * Set system properties for admin server instance. Some of the admin code
581      * relies on the property <code>com.sun.aas.javaRoot</code>. This method
582      * will initialize the property using the value set by JVM plugin. If the
583      * property is already set, it will not be changed.
584      * /
585     private void setAdminInstanceProperties() {
586         try {
587             String adminJavaRoot = System.getProperty(ADMIN_JAVAROOT);
588             String pluginJavaRoot = System.getProperty(PLUGIN_JAVAROOT);
589             if (adminJavaRoot == null) {
590                 if (pluginJavaRoot != null) {
591                     System.setProperty(ADMIN_JAVAROOT, pluginJavaRoot);
592                 } else {
593                     sLogger.log(Level.WARNING, "core.no_java_home");
594                 }
595             }
596         } catch (Throwable t) {
597             sLogger.log(Level.WARNING, "core.set_admin_property_failed");
598             sLogger.log(Level.FINE, "general.unexpected_exception", t);
599         }
600     }
601     */

602     
603     /**
604      * This method determines whether it is running as DAS or DASD or INSTANCE
605      *
606      * This has to be called after AdminServiceMBean has been initialized.
607      *
608      * @return "DAS" or "DASD" or "INSTANCE"
609      */

610     private static String JavaDoc getAdminType() {
611        
612         if(adminType != null) return adminType;
613         
614         //get Admin ServiceMBean
615

616         //Determine the type of admin server
617
adminType = "DAS"; //FIXME NYI
618
//cache the value in adminType variable.
619
return adminType;
620     }
621     
622
623         /**
624      * Create and configure the registry of managed objects.
625      */

626     private static void createRegistry() {
627         System.out.println("Create configuration registry ...");
628         try {
629             URL JavaDoc url = AdminService.class.getResource
630                 ("/mbeans-descriptors.xml");
631             InputStream JavaDoc stream = url.openStream();
632             MBeanRegistry registry = new MBeanRegistry();
633             registry.loadMBeanRegistry(stream);
634             stream.close();
635         } catch (Throwable JavaDoc t) {
636             t.printStackTrace(System.out);
637             System.exit(1);
638         }
639
640     }
641
642       /**
643      * Create the tree of managed objects.
644      */

645     private void createConfigContext() {
646         //FIXME: load from variables.
647
try {
648             //ConfigFactory.createConfigContext("/home/sridatta/jws/appserv-admin-core/dtds/domain.xml");
649
ConfigFactory.createConfigContext("../config-api/dtds/domain.xml");
650         } catch (Throwable JavaDoc t) {
651             t.printStackTrace(System.out);
652             System.exit(1);
653         }
654
655     }
656
657     //FIXME: load properties from outside
658
//get domain name from somewhere.
659
private void createMBS() {
660
661         System.out.println("Creating MBeanServer ...");
662         try {
663             System.setProperty("javax.management.builder.initial",
664                     "com.sun.enterprise.admin.jmx.AppServerMBeanServerBuilder");
665             
666             //FIXME: we may want to cache MBS in a static variable since it needs to be accessed again.
667
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer("com.sun.appserv");
668         } catch (Throwable JavaDoc t) {
669             t.printStackTrace(System.out);
670             System.exit(1);
671         }
672
673     }
674
675      private void createAdminServiceMBean() {
676
677         System.out.println("Creating AdminServiceMBean ...");
678         try {
679             //load admin service mbean
680
//nyi
681
;
682         } catch (Throwable JavaDoc t) {
683             t.printStackTrace(System.out);
684             System.exit(1);
685         }
686
687     }
688   
689      /**
690       * This method is called when AdminType == DAS or DASD only
691       */

692      private void registerAdminMBeans() {
693          //FIXME: keep reference to mom. or get it from switch. later.
694
ConfigMBeansManager cmbm = new ConfigMBeansManager();
695          ManagementObjectManager mom = new J2EEManagementObjectManager();
696          
697          //FIXME get domain name from admin service?
698
String JavaDoc domain = "com.sun.appserv";
699          mom.registerJ2EEDomain(domain);
700          cmbm.registerAllConfigJ2EEServers();
701      }
702      
703      //FIXME REMOVED
704
// private static final String ADMIN_JAVAROOT = "com.sun.aas.javaRoot";
705
// private static final String PLUGIN_JAVAROOT = "JAVA_HOME";
706
}
707
Popular Tags