KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > ManagedAdminServerInstance


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.server.core.mbean.config;
25
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.net.InetAddress JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33
34 //Config imports
35
import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
36 import com.sun.enterprise.config.serverbeans.ServerTags;
37 import com.sun.enterprise.config.ConfigContext;
38 import com.sun.enterprise.config.ConfigException;
39 import com.sun.enterprise.config.serverbeans.Server;
40 import com.sun.enterprise.config.serverbeans.Config;
41 import com.sun.enterprise.config.serverbeans.HttpService;
42 import com.sun.enterprise.config.serverbeans.HttpListener;
43
44 //Other imports
45
import com.sun.enterprise.instance.InstanceEnvironment;
46 import com.sun.enterprise.instance.ServerManager;
47
48 //Admin imports
49
import com.sun.enterprise.admin.util.HostAndPort;
50 import com.sun.enterprise.admin.common.ObjectNames;
51 import com.sun.enterprise.admin.common.constant.AdminConstants;
52 import com.sun.enterprise.admin.common.constant.ConfigAttributeName;
53 import com.sun.enterprise.admin.common.exception.ServerInstanceException;
54 import com.sun.enterprise.admin.common.exception.PortInUseException;
55 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
56 /* New for 8.0 */
57 import com.sun.enterprise.admin.server.core.mbean.config.Domain2ServerTransformer;
58 import com.sun.enterprise.server.Constants;
59 /* New for 8.0 */
60
61 /**
62     The MBean that represents the Admin Server Instance for iAS SE. In
63     other words it represents the <strong> management interface </strong> of
64     the Admin Server Instance.
65     ObjectName of this MBean is ias:type=server-instance, name=admin-server
66 */

67
68 public class ManagedAdminServerInstance extends ConfigMBeanBase
69 {
70     private static final String JavaDoc[][] MAPLIST =
71     {
72    // REMOVED FROM DTD from server element
73
// {ConfigAttributeName.Server.kLocale ,
74
// ConfigMBeanBase.ATTRIBUTE + ServerTags.LOCALE}
75
};
76
77     private static final String JavaDoc[] ATTRIBUTES =
78     {
79    // ConfigAttributeName.Server.kLocale + ", String, RW"
80
};
81
82     private static final String JavaDoc[] OPERATIONS =
83     {
84         "applyConfigChanges(), ACTION",
85         "useManualConfigChanges(), ACTION",
86         "getHostAndPort(), INFO",
87         "isApplyNeeded(), INFO",
88 // "getCertNicknames(), INFO",
89
"isRestartNeeded(), INFO"
90     };
91
92     private static final Logger JavaDoc _logger = Logger.getLogger(
93             AdminConstants.kLoggerName);
94
95     private ManagedServerInstance mDelegate;
96
97     private boolean mRestartNeeded = false;
98
99     public ManagedAdminServerInstance() throws MBeanConfigException
100     {
101         this.setDescriptions(MAPLIST, ATTRIBUTES, OPERATIONS);
102         this.initialize(ObjectNames.kServerInstance, new String JavaDoc[]{ServerManager.ADMINSERVER_ID});
103     }
104
105     /**
106      * Copies the file from backup directory to the real config directory
107      * so that the configuration is stored on disk. There is no guarantee of
108      * any transactional support. In case of admin server instance only
109      * server.xml file is edited in the backup directory, so this method only
110      * copies server.xml. (The other conf files for admin instance are edited
111      * directly in live configuration area). As there is no dynamic
112      * reconfiguration for admin server, the admin instance always requires
113      * restart for new settings to take affect.
114      * @return True means requires restart.
115      */

116     public boolean applyConfigChanges() throws ServerInstanceException
117     {
118         try {
119             InstanceEnvironment ie = new InstanceEnvironment(
120                     ServerManager.ADMINSERVER_ID);
121             // Force write of server.xml changes because the check to ensure
122
// that manual edits are not lost (the method canApplyConfigChanges)
123
// is already invoked from MBeanServer just before invoking this
124
// (applyConfigChanges) method.
125
//ie.applyServerXmlChanges(true);
126
initDelegate();
127             ConfigContext ctx = mDelegate.getConfigContext(
128                     ServerManager.ADMINSERVER_ID);
129             ctx.resetConfigChangeList();
130             mRestartNeeded = true;
131             /* New for 8.0 - temporary - gen server.xml b4 notif */
132             /* This call most likely goes away */
133             if (ManagedServerInstance.PORT_DOMAIN_TO_SERVER) {
134                 final String JavaDoc domainXMLPath = ie.getConfigFilePath();
135                 final String JavaDoc serverXMLPath = System.getProperty(Constants.IAS_ROOT)
136                     + "/"
137                     + ie.getName()
138                     + "/config/"
139                     + ie.kServerXMLFileName;
140                 new Domain2ServerTransformer(domainXMLPath, serverXMLPath).transform();
141             }
142             /* New for 8.0 - temporary - gen server.xml b4 notif */
143         } catch (ConfigException ce) {
144             _logger.log(Level.INFO, "mbean.config.admin.apply_failed",
145                     ce.getLocalizedMessage());
146             _logger.log(Level.FINEST, ce.getMessage(), ce);
147             throw new ServerInstanceException(ce.getLocalizedMessage());
148         }
149         return mRestartNeeded;
150     }
151
152     /**
153      * Can config changes be applied. This method returns <code>true</code>
154      * <UL>
155      * <LI>if live server.xml has not been manually edited or</LI>
156      * <LI>if live server.xml has been manully edited but no other changes
157      * have been made through the GUI or CLI to backup server.xml.</LI></UL>.
158      * This method is explicitly coded to not throw any exception and will
159      * return false in case there is any exception in processing.
160      * @return true if config changes can be applied, false otherwise
161      */

162     public boolean canApplyConfigChanges()
163     {
164         boolean canApply = false;
165         try {
166             initDelegate();
167             InstanceEnvironment ie = new InstanceEnvironment(
168                     ServerManager.ADMINSERVER_ID);
169             /*
170             if (ie.hasHotXmlChanged()) {
171                 if (ie.canReloadManualXmlChanges()) {
172                     ie.useManualServerXmlChanges();
173                     mDelegate.reloadAfterChange(ie);
174                     canApply = true;
175                 }
176             } else {
177              */

178                 canApply = true;
179                 /*
180             }
181                  **/

182         } catch (Exception JavaDoc ex) {
183             _logger.log(Level.INFO, "mbean.config.admin.canapply_failed",
184                     ex.getLocalizedMessage());
185             _logger.log(Level.FINEST, ex.getLocalizedMessage(), ex);
186         }
187         return canApply;
188     }
189
190     /**
191      * Use manual config changes. This method is invoked if the user decides
192      * to keep the hand edits to server.xml.
193      * @return true if restart is required.
194      */

195     public boolean useManualConfigChanges() throws ServerInstanceException
196     {
197         try {
198             initDelegate();
199             InstanceEnvironment ie =
200                     new InstanceEnvironment(ServerManager.ADMINSERVER_ID);
201             // copy server.xml from hot to back
202
//ie.useManualServerXmlChanges();
203
// Reinitialize mbeans
204
mDelegate.reloadAfterChange(ie);
205             // Set restart needed to true because we do not know whether admin
206
// server is already running with the changes in server.xml -- the
207
// policy is say restart is needed unless you know otherwise.
208
mRestartNeeded = true;
209         } catch (ConfigException ce) {
210             _logger.log(Level.INFO, "mbean.config.admin.usemanual_failed",
211                     ce.getMessage());
212             _logger.log(Level.FINEST, ce.getMessage(), ce);
213             throw new ServerInstanceException(ce.getLocalizedMessage());
214         }
215         return mRestartNeeded;
216     }
217
218     /**
219      * Is apply changes required. This method added to sync up with
220      * ManagedServerInstance. SOM uses same object for admin server mbean
221      * non admin server mbean. Therefore the method signatures should be same.
222      * @param check not used
223      */

224     public boolean isApplyNeeded(boolean check) throws ServerInstanceException {
225         return isApplyNeeded();
226     }
227
228     /**
229      * Is apply changes required. This method returns true if any changes
230      * have been made to server.xml that have not been applied yet.
231      * @return true if there are changes to backup server.xml and that have
232      * not been applied to live server.xml
233      */

234     public boolean isApplyNeeded() throws ServerInstanceException
235     {
236         boolean applyNeeded = false;
237         try
238         {
239             initDelegate();
240             ConfigContext ctx = mDelegate.getConfigContext(
241                     ServerManager.ADMINSERVER_ID);
242             applyNeeded = ctx.isChanged();
243         } catch (ConfigException ce) {
244             _logger.log(Level.INFO, "mbean.config.admin.applyneeded_failed",
245                     ce.getMessage());
246             _logger.log(Level.FINEST, ce.getMessage(), ce);
247             throw new ServerInstanceException(ce.getLocalizedMessage());
248         }
249         return applyNeeded;
250     }
251
252     /**
253      * Is restart needed. For admin server instance, there is no dynamic
254      * reconfiguration, so restart is needed whenever any change is applied.
255      * @return true if restart is required.
256      */

257     public boolean isRestartNeeded() throws ServerInstanceException
258     {
259         return mRestartNeeded;
260     }
261
262     /**
263      */

264     public HostAndPort getHostAndPort() throws ServerInstanceException
265     {
266         return getHttpListenerHostPort();
267     }
268
269     private synchronized void initDelegate() throws ServerInstanceException
270     {
271         if (mDelegate != null)
272         {
273             return;
274         }
275         try
276         {
277             HostAndPort hp = getHttpListenerHostPort();
278             mDelegate = new ManagedServerInstance(ServerManager.ADMINSERVER_ID,
279                                                   hp, false);
280         }
281         catch (ServerInstanceException sie)
282         {
283             throw sie;
284         }
285         catch (PortInUseException piue)
286         {
287         }
288         catch (Exception JavaDoc e)
289         {
290             throw new ServerInstanceException(e.getLocalizedMessage());
291         }
292     }
293
294     /**
295      * Returns the host & port of the http listener 'http-listener-1'.
296      * (host by default is localhost).
297      */

298     private HostAndPort getHttpListenerHostPort()
299         throws ServerInstanceException
300     {
301         HostAndPort hAndp = null;
302         try
303         {
304 //ms1 Server server = (Server) super.getBaseConfigBean();
305
Config config = (Config) super.getConfigBeanByXPath(ServerXPathHelper.XPATH_CONFIG);
306             HttpService https = config.getHttpService();
307             
308             HttpListener[] hlArray = https.getHttpListener();
309             //check not needed since there should always be atleast 1 httplistener
310
//if you don't find one, use first one.
311
HttpListener ls = hlArray[0];
312             //default is the first one that is enabled.
313
for(int i = 0;i<hlArray.length;i++) {
314                 if(hlArray[i].isEnabled()) {
315                     ls = hlArray[i];
316                     break;
317                 }
318             }
319             
320             String JavaDoc port = ls.getPort();
321             int intPort = Integer.parseInt (port);
322             hAndp = new HostAndPort("localhost", intPort);
323         }
324         catch (Exception JavaDoc e)
325         {
326             throw new ServerInstanceException(e.getLocalizedMessage());
327         }
328         return hAndp;
329     }
330
331
332 }
333
Popular Tags