KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > storage > MBeanManufacturer


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.admin.server.core.jmx.storage;
24
25 //JMX imports
26
import javax.management.ObjectName JavaDoc;
27 //Admin imports
28
import com.sun.enterprise.admin.AdminContext;
29 import com.sun.enterprise.admin.common.ObjectNames;
30 import com.sun.enterprise.admin.common.ObjectNameHelper;
31 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
32 import com.sun.enterprise.admin.util.HostAndPort;
33 //import com.sun.enterprise.admin.server.core.mbean.config.*;
34
import com.sun.enterprise.admin.common.constant.AdminConstants;
35 import com.sun.enterprise.admin.server.core.mbean.config.GenericConfigurator;
36 import com.sun.enterprise.admin.server.core.mbean.config.ServerController;
37 import com.sun.enterprise.admin.server.core.mbean.config.ManagedAdminServerInstance;
38 import com.sun.enterprise.admin.server.core.mbean.config.ManagedServerInstance;
39 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo;
40
41 //Other imports
42
import com.sun.enterprise.config.serverbeans.*;
43 import com.sun.enterprise.config.ConfigFactory;
44 import com.sun.enterprise.config.ConfigContext;
45 import com.sun.enterprise.config.ConfigException;
46 import com.sun.enterprise.config.ConfigBeansFactory;
47 import com.sun.enterprise.instance.ServerManager;
48
49 import com.sun.enterprise.admin.meta.MBeanRegistryEntry;
50 //server
51
import com.sun.enterprise.server.ApplicationServer;
52 import com.sun.enterprise.instance.InstanceEnvironment;
53
54 import java.util.logging.Level JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56
57 //i18n import
58
import com.sun.enterprise.util.i18n.StringManager;
59
60
61 /**
62     A class to construct proper type of MBean from its object name. This
63     class gives the instances of mbeans that would be registered as given
64     object names.
65 */

66
67 public class MBeanManufacturer
68 {
69     private ObjectName JavaDoc mObjectName = null;
70     private Object JavaDoc mConfigBean = null;
71     private AdminContext mAdminContext;
72
73     private static final Logger JavaDoc _logger = Logger.getLogger(AdminConstants.kLoggerName);
74     
75     // i18n StringManager
76
private static StringManager localStrings =
77         StringManager.getManager( MBeanManufacturer.class );
78     
79
80     public MBeanManufacturer (ObjectName JavaDoc oName, Object JavaDoc configBean)
81     {
82         if (oName == null || configBean == null)
83         {
84             String JavaDoc msg = localStrings.getString( "admin.server.core.jmx.storage.null_object_name_or_config_bean" );
85             throw new IllegalArgumentException JavaDoc( msg );
86         }
87         mObjectName = oName;
88         mConfigBean = configBean;
89     }
90
91     public void setAdminContext(AdminContext ctx) {
92         mAdminContext = ctx;
93     }
94
95     /**
96         Returns an instance of proper MBean. This is required, when somebody
97         wants to create the MBean for the first time in MBean Repository.
98         The necessary parameters for MBean construction are derived from the
99         configBean passed during consruction of this class.\
100         @return instance of proper MBean corresponding to ObjectName. ObjectName
101         is passed during the construction of this class. May not be null.
102     */

103     public Object JavaDoc createMBeanInstance()
104     {
105         Object JavaDoc mbeanInstance = null;
106         String JavaDoc instanceName = ApplicationServer.getServerContext().getInstanceName();
107         
108         //8.0 first
109
if(mConfigBean instanceof MBeanRegistryEntry)
110         {
111             ConfigContext configContext;
112             try
113             {
114                 if (mAdminContext != null) {
115                     configContext = mAdminContext.getAdminConfigContext();
116                 } else {
117                     InstanceEnvironment instanceEnvironment = new InstanceEnvironment(instanceName);
118                     //String fileUrl = instanceEnvironment.getConfigFilePath();
119
/*Everything should be set in the backup file*/
120                     String JavaDoc fileUrl = instanceEnvironment.getBackupConfigFilePath();
121                     configContext = ConfigFactory.createConfigContext(fileUrl);
122                 }
123                 return ((MBeanRegistryEntry)mConfigBean).instantiateMBean(mObjectName,null, configContext);
124             }
125             catch(Exception JavaDoc e)
126             {
127                 return null;
128             }
129         }
130         String JavaDoc type = ObjectNameHelper.getType(mObjectName);
131         //String instanceName = ObjectNameHelper.getServerInstanceName(mObjectName);
132
Level JavaDoc logLevel = Level.SEVERE;
133
134         try
135         {
136             if (type.equals(ObjectNames.kController))
137             {
138                 mbeanInstance = new ServerController(mAdminContext);
139             }
140             else if (type.equals(ObjectNames.kGenericConfigurator))
141             {
142                 mbeanInstance = new GenericConfigurator();
143             }
144             else if (type.equals(ObjectNames.kServerInstance))
145             {
146                 mbeanInstance = createServerInstanceMBean(instanceName);
147 /* *************** **
148                 if (instanceName.equals(ServerManager.ADMINSERVER_ID))
149                 {
150                     mbeanInstance = createAdminServerInstance();
151                 }
152                 else
153                 {
154                     mbeanInstance = createServerInstanceMBean(instanceName);
155                 }
156 ** *************** */

157             }
158             else
159             {
160                 logLevel = Level.FINE;
161                 mbeanInstance = createGenericConfigMBean(mObjectName);
162             }
163         }
164         catch(Exception JavaDoc e)
165         {
166             _logger.log(logLevel, "mbean.config.admin.create_mbean_instance_failed", e );
167         }
168         
169         return ( mbeanInstance );
170     }
171     
172     private ManagedServerInstance createServerInstanceMBean(String JavaDoc instanceName) throws Exception JavaDoc
173     {
174         Server server = (Server)mConfigBean;
175 //patch for ms1
176
ConfigContext ctx = server.getConfigContext();
177 Config config = (Config) ConfigBeansFactory.getConfigBeanByXPath(ctx, ServerXPathHelper.XPATH_CONFIG);
178         HttpService https = config.getHttpService();
179         
180         HttpListener[] hlArray = https.getHttpListener();
181         //check not needed since there should always be atleast 1 httplistener
182
//if you don't find one, use first one.
183
HttpListener ls = hlArray[0];
184         //default is the first one that is enabled.
185
for(int i = 0;i<hlArray.length;i++) {
186             if(hlArray[i].isEnabled()) {
187                 ls = hlArray[i];
188                 break;
189             }
190         }
191         String JavaDoc port = new PropertyResolver(ctx, instanceName).
192                 resolve(ls.getPort());
193         int intPort = Integer.parseInt (port);
194         HostAndPort hp = new HostAndPort("localhost", intPort);
195         return new ManagedServerInstance(instanceName, hp, false, mAdminContext);
196     }
197
198     private ManagedAdminServerInstance createAdminServerInstance()
199         throws Exception JavaDoc
200     {
201         return new ManagedAdminServerInstance();
202     }
203
204     private Object JavaDoc createGenericConfigMBean(ObjectName JavaDoc objectName) throws MBeanConfigException
205     {
206         ConfigMBeanNamingInfo mbeanInfo = new ConfigMBeanNamingInfo(objectName);
207         mbeanInfo.setAdminContext(mAdminContext);
208         return mbeanInfo.constructConfigMBean();
209     }
210
211 }
212
Popular Tags