001: /**
002: * $Id: PortalServerInstance.java,v 1.26 2006/11/03 22:19:38 cathywu Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.mbeans;
014:
015: import javax.management.MBeanException;
016: import javax.management.MBeanServerConnection;
017: import javax.management.ObjectName;
018: import javax.management.remote.JMXConnector;
019:
020: import java.util.*;
021: import java.util.logging.Level;
022: import java.util.logging.Logger;
023: import java.util.logging.LogRecord;
024: import java.io.File;
025: import java.io.FileWriter;
026: import java.io.IOException;
027:
028: import com.sun.portal.log.common.PortalLogger;
029: import com.sun.portal.log.common.PortalLogManager;
030: import com.sun.portal.admin.server.AdminServerUtil;
031: import com.sun.portal.admin.server.PASModule;
032: import com.sun.portal.admin.server.mbeans.PSResource;
033: import com.sun.portal.admin.common.util.AdminUtil;
034: import com.sun.portal.admin.common.InstanceAttributes;
035: import com.sun.portal.admin.common.PortalAttributes;
036: import com.sun.portal.admin.common.PSMBeanException;
037: import com.sun.portal.admin.common.context.PortalDomainContext;
038: import com.sun.portal.admin.common.context.PortalDomainContextException;
039: import com.sun.portal.admin.common.context.PSConfigContext;
040: import com.sun.portal.fabric.util.FileUtil;
041: import com.sun.portal.fabric.util.XMLJdomUtil;
042: import com.sun.portal.fabric.util.XMLJdomFileCreationException;
043: import com.sun.portal.fabric.tasks.ConfigurationException;
044: import com.sun.portal.fabric.tasks.WebContainer;
045: import com.sun.portal.fabric.tasks.WebContainerFactory;
046:
047: public class PortalServerInstance extends PSResource implements
048: PortalServerInstanceMBean {
049:
050: private static Logger logger = PortalLogger
051: .getLogger(PortalServerInstance.class);
052: private static final int ADD_URI = 1;
053: private static final int REMOVE_URI = 2;
054: private ObjectName iObjName = null;
055: private String host = null;
056: private String port = null;
057: private String warDirLocation = null;
058: private Boolean isLocal = Boolean.FALSE;
059: private WebContainer wcInstance = null;
060: private String sPortalID = null;
061:
062: public void init(PSConfigContext cc, PortalDomainContext pdc,
063: List path) {
064: super .init(cc, pdc, path);
065:
066: try {
067: iObjName = AdminUtil.getResourceMBeanObjectName(TYPE, path);
068: host = pdc.getAttributeValue(TYPE, path,
069: InstanceAttributes.HOST);
070: port = pdc.getAttributeValue(TYPE, path,
071: InstanceAttributes.PORT);
072: isLocal = AdminUtil.isLocal(host) ? Boolean.TRUE
073: : Boolean.FALSE;
074:
075: // Portal ID
076: sPortalID = (String) path.get(1);
077:
078: // Set the webapp directory for the portal
079: warDirLocation = cc.getPSDataDir() + fs + "portals" + fs
080: + sPortalID + fs + "war";
081:
082: // Initialize the webcontainer instance
083: setWCInstance();
084: } catch (Exception e) {
085: logger.log(Level.SEVERE, "PSFB_CSPFM1105", e);
086: throw new RuntimeException(e);
087: }
088: }
089:
090: public void redeploy(String uri) throws PSMBeanException {
091: }
092:
093: public void deployAll() throws PSMBeanException {
094:
095: if (!isLocal.booleanValue()) {
096: invokeRemoteOperation(host, iObjName, "deployAll", null,
097: null);
098: return;
099: }
100:
101: File warDir = new File(warDirLocation);
102: Vector warFiles = new Vector();
103: // Fill the vector with the set of war files in the war directory
104: FileUtil.getFiles(warDir, warFiles, "war");
105:
106: int iNumFiles = (warFiles != null) ? warFiles.size() : 0;
107: for (int i = 0; i < iNumFiles; i++) {
108: File war = (File) warFiles.get(i);
109: String fileName = war.getName();
110: logger.log(Level.INFO, "PSFB_CSPFM1123",
111: new String[] { fileName });
112: int idx = fileName.indexOf(".war");
113: String uri = "/" + fileName.substring(0, idx);
114: try {
115: wcInstance.justDeploy(war.getAbsolutePath(), uri);
116: updateListOfDeployedWebapps(pdc, uri, ADD_URI);
117: } catch (ConfigurationException e) {
118: logger.log(Level.SEVERE, "PSFB_CSPFM1107", e);
119: throw new PSMBeanException("admin.error.deploy.failed",
120: e.getMessage(), e, null);
121: } catch (Exception e) {
122: logger.log(Level.SEVERE, "PSFB_CSPFM1108", e);
123: throw new PSMBeanException("admin.error.set.resources",
124: e.getMessage(), e, null);
125: }
126: }
127: if (iNumFiles > 0) {
128: try {
129: wcInstance.postDeploy();
130: } catch (ConfigurationException ce) {
131: logger.log(Level.SEVERE, "PSFB_CSPFM1107", ce);
132: throw new PSMBeanException("admin.error.deploy.failed",
133: ce.getMessage(), ce, null);
134: }
135: }
136:
137: }
138:
139: public void deploy(String uri) throws PSMBeanException {
140:
141: if (!isLocal.booleanValue()) {
142: Object[] params = { uri };
143: String[] signature = { "java.lang.String" };
144:
145: invokeRemoteOperation(host, iObjName, "deploy", params,
146: signature);
147: return;
148: }
149:
150: // Lookup for a .war file with the name of the uri thats specified
151: // under the war directory. If it does not exist abort.
152: File war = new File(warDirLocation + fs + uri + ".war");
153: if (war.exists()) {
154:
155: try {
156: wcInstance.deploy(war.getAbsolutePath(), uri);
157: updateListOfDeployedWebapps(pdc, uri, ADD_URI);
158: } catch (ConfigurationException ce) {
159: logger.log(Level.SEVERE, "PSFB_CSPFM1107", ce);
160: throw new PSMBeanException("admin.error.deploy.failed",
161: ce.getMessage(), ce, null);
162: } catch (Exception e) {
163: logger.log(Level.SEVERE, "PSFB_CSPFM1108", e);
164: throw new PSMBeanException("admin.error.set.resources",
165: e.getMessage(), e, null);
166: }
167: } else {
168: throw new PSMBeanException("admin.error.deploy.failed",
169: "WAR File Not Found :" + war);
170: }
171: }
172:
173: public void undeploy(String uri) throws PSMBeanException {
174: if (!isLocal.booleanValue()) {
175: Object[] params = { uri };
176: String[] signature = { "java.lang.String" };
177: invokeRemoteOperation(host, iObjName, "undeploy", params,
178: signature);
179: return;
180: }
181:
182: try {
183: wcInstance.undeploy(uri);
184:
185: updateListOfDeployedWebapps(pdc, uri, REMOVE_URI);
186:
187: } catch (ConfigurationException ce) {
188: logger.log(Level.SEVERE, "PSFB_CSPFM1110", ce);
189: throw new PSMBeanException("admin.error.undeploy.failed",
190: ce.getMessage(), ce, null);
191: } catch (Exception e) {
192: logger.log(Level.SEVERE, "PSFB_CSPFM1109", e);
193: throw new PSMBeanException("admin.error.set.resources", e
194: .getMessage(), e, null);
195: }
196: }
197:
198: public void undeployAll() throws PSMBeanException {
199: if (!isLocal.booleanValue()) {
200: invokeRemoteOperation(host, iObjName, "undeployAll", null,
201: null);
202: return;
203: }
204: try {
205: // Get the set of already deployed web application URIs
206: Iterator uriValues = pdc.getAttributeValues(TYPE, path,
207: InstanceAttributes.DEPLOYED_WEBAPPS).iterator();
208:
209: // If this is the first webpp then the set will be empty
210: // Initialize it
211: while (uriValues.hasNext()) {
212: String uri = (String) uriValues.next();
213: logger.log(Level.INFO,
214: "PortalServerInstance.undeploy(), web app:"
215: + uri);
216: undeploy(uri);
217: }
218: } catch (PSMBeanException ce) {
219: logger.log(Level.SEVERE, "PSFB_CSPFM1110", ce);
220: throw new PSMBeanException("admin.error.undeploy.failed",
221: ce.getMessage(), ce, null);
222: } catch (Exception e) {
223: logger.log(Level.SEVERE, "PSFB_CSPFM1109", e);
224: throw new PSMBeanException("admin.error.set.resources", e
225: .getMessage(), e, null);
226: }
227: }
228:
229: public Boolean configureContainer(String confDir, String instanceID)
230: throws PSMBeanException {
231: boolean ret = true;
232: File fwcConfigData = null;
233: try {
234: List portalPath = new ArrayList();
235: int iSize = (path != null) ? path.size() : 0;
236: for (int i = iSize - 2; i < iSize; i++) {
237: portalPath.add(path.get(i));
238: }
239:
240: // Read the Web Container Config Data from the PAS and store it in a file
241: // <install_loc>/SUNWportal/portals/<portal_id>/config/WebContainerConfigData.xml
242: String swcConfigDataR = null;
243: try {
244: logger.log(Level.INFO, "Type=" + PortalMBean.TYPE);
245: logger.log(Level.INFO, "List=" + portalPath);
246: swcConfigDataR = pdc.getAttributeValue(
247: PortalMBean.TYPE, portalPath,
248: PortalAttributes.WEB_CONTAINER_CONFIG_DATA);
249: } catch (PortalDomainContextException pde) {
250: // Ignore it. The attribute may not exist.
251: swcConfigDataR = null;
252: }
253:
254: String swcConfigDataFile = cc.getPSDataDir() + fs
255: + "portals" + fs + sPortalID + fs + "config" + fs
256: + "WebContainerConfigData.xml";
257: fwcConfigData = new File(swcConfigDataFile);
258: if (fwcConfigData.exists()) {
259:
260: fwcConfigData.delete();
261: }
262:
263: logger.log(Level.INFO, "PSFB_CSPFM1020", new String[] {
264: "Configure Portal Apps",
265: (swcConfigDataR == null) ? "" : swcConfigDataR });
266:
267: long lOldFileLength = 0;
268: if ((swcConfigDataR != null)
269: && (swcConfigDataR.trim().length() > 0)) {
270:
271: FileWriter fw = new FileWriter(fwcConfigData);
272: fw.write(swcConfigDataR);
273: fw.close();
274: lOldFileLength = fwcConfigData.length();
275: }
276:
277: ret = wcInstance.configureApp(confDir, cc);
278:
279: // Write the Web Container Config Data to the PAS.
280: fwcConfigData = new File(swcConfigDataFile);
281: long lNewFileLength = fwcConfigData.length();
282: if (lOldFileLength != lNewFileLength) {
283:
284: String swcConfigDataW = null;
285: try {
286: XMLJdomUtil xmlJdomUtil = new XMLJdomUtil();
287: xmlJdomUtil
288: .readXMLFileLessSchemaValidation(swcConfigDataFile);
289: swcConfigDataW = xmlJdomUtil
290: .getXMLJdomDocumentAsString();
291: } catch (XMLJdomFileCreationException e0) {
292: logger.log(Level.SEVERE, e0.getMessage());
293: swcConfigDataW = null;
294: }
295:
296: if ((swcConfigDataW != null)
297: && (swcConfigDataW.trim().length() > 0)) {
298:
299: // Set the Web Conatainer Config Data to PAS
300: logger.log(Level.INFO, "PSFB_CSPFM1021",
301: new String[] {
302: "Configure Portal Apps",
303: (swcConfigDataW == null) ? ""
304: : swcConfigDataW });
305: pdc.setAttributeValue(PortalMBean.TYPE, portalPath,
306: PortalAttributes.WEB_CONTAINER_CONFIG_DATA,
307: swcConfigDataW);
308: } else {
309:
310: ResourceBundle resourceBundle = logger.getLogger(
311: PortalLogManager.DEBUG_ROOT_LOGGER)
312: .getResourceBundle();
313: String msg = resourceBundle
314: .getString("PSFB_CSPFM1022");
315: XMLJdomFileCreationException e1 = new XMLJdomFileCreationException(
316: msg);
317: throw new PSMBeanException(
318: "admin.error.configure.instance", msg, e1);
319: }
320: }
321: } catch (PortalDomainContextException pde0) {
322: logger.log(Level.SEVERE, "PSFB_CSPFM1111", pde0);
323: throw new PSMBeanException(
324: "admin.error.configure.instance",
325: pde0.getMessage(), pde0,
326: new Object[] { instanceID });
327: } catch (IOException ioe) {
328: logger.log(Level.SEVERE, "PSFB_CSPFM1030", ioe);
329: throw new PSMBeanException(
330: "admin.error.configure.instance", ioe.getMessage(),
331: ioe, new Object[] { instanceID });
332: } catch (ConfigurationException ex) {
333: ret = false;
334: if (logger.isLoggable(Level.SEVERE)) {
335: LogRecord rec = new LogRecord(Level.SEVERE,
336: "PSFB_CSPFM1112");
337: rec.setLoggerName(logger.getName());
338: rec.setParameters(new Object[] { confDir });
339: rec.setThrown(ex);
340: logger.log(rec);
341: }
342: } finally {
343: if ((fwcConfigData != null) && fwcConfigData.isFile()) {
344:
345: fwcConfigData.delete();
346: }
347: }
348:
349: return new Boolean(ret);
350: }
351:
352: public String getHost() {
353: return host;
354: }
355:
356: public String getPort() {
357: return port;
358: }
359:
360: public Boolean isLocal() {
361: return isLocal;
362: }
363:
364: public void restart() throws PSMBeanException {
365: try {
366: logger.log(Level.SEVERE, "PSFB_CSPFM1113");
367: stop();
368: logger.log(Level.SEVERE, "PSFB_CSPFM1114");
369: } catch (Exception ce) {
370: logger.log(Level.SEVERE, "PSFB_CSPFM1115", ce);
371: throw new PSMBeanException("admin.error.stop.instance", ce
372: .getMessage(), ce, new Object[] { getID() });
373: }
374:
375: try {
376: logger.log(Level.SEVERE, "PSFB_CSPFM1116");
377: start();
378: logger.log(Level.SEVERE, "PSFB_CSPFM1117");
379: } catch (Exception ce) {
380: logger.log(Level.SEVERE, "PSFB_CSPFM1118", ce);
381: throw new PSMBeanException("admin.error.start.instance", ce
382: .getMessage(), ce, new Object[] { getID() });
383: }
384: }
385:
386: public void start() throws PSMBeanException {
387: if (!isLocal.booleanValue()) {
388: invokeRemoteOperation(host, iObjName, "start", null, null);
389: return;
390: }
391:
392: try {
393: wcInstance.start();
394: } catch (ConfigurationException ce) {
395: logger.log(Level.SEVERE, "PSFB_CSPFM1118", ce);
396: throw new PSMBeanException("admin.error.start.instance", ce
397: .getMessage(), ce, new Object[] { getID() });
398: }
399: }
400:
401: public void stop() throws PSMBeanException {
402: if (!isLocal.booleanValue()) {
403: invokeRemoteOperation(host, iObjName, "stop", null, null);
404: return;
405: }
406:
407: try {
408: wcInstance.stop();
409: } catch (ConfigurationException ce) {
410: logger.log(Level.SEVERE, "PSFB_CSPFM1115", ce);
411: throw new PSMBeanException("admin.error.stop.instance", ce
412: .getMessage(), ce, new Object[] { getID() });
413: }
414: }
415:
416: public String getMBeanServerConnectionInfo()
417: throws PSMBeanException {
418: try {
419: String encrypted = pdc.getAttributeValue(TYPE, path,
420: InstanceAttributes.MBEAN_SERVER_CONNECTION_INFO);
421:
422: return AdminServerUtil.decrypt(encrypted);
423: } catch (Exception e) {
424: logger.log(Level.SEVERE, "PSFB_CSPFM1119", e);
425: throw new PSMBeanException(
426: "admin.error.get.connection.info", e.getMessage(),
427: e, new Object[] { getID() });
428: }
429: }
430:
431: public void setMBeanServerConnectionInfo(String connectionInfo)
432: throws PSMBeanException {
433:
434: try {
435: String encrypted = AdminServerUtil.encrypt(connectionInfo);
436:
437: pdc.setAttributeValue(TYPE, path,
438: InstanceAttributes.MBEAN_SERVER_CONNECTION_INFO,
439: encrypted);
440: } catch (Exception e) {
441: logger.log(Level.SEVERE, "PSFB_CSPFM1120", e);
442: throw new PSMBeanException(
443: "admin.error.set.connection.info", e.getMessage(),
444: e, new Object[] { getID() });
445: }
446: }
447:
448: private void setWCInstance() throws PSMBeanException {
449:
450: Map wcInfoMap = new HashMap(6);
451: // Get the webcontainer attributes from PDC and set into Map
452: for (int i = 0; i < WebContainer.wcAttrKeys.length; i++) {
453: String attrVal = "";
454: String attrKey = WebContainer.wcAttrKeys[i];
455: try {
456: attrVal = pdc.getAttributeValue(TYPE, path, attrKey);
457: } catch (PortalDomainContextException pde) {
458: // Probably the attribute does not exit
459: // Ignore it
460: }
461: if (attrVal != null && attrVal.length() > 0) {
462: wcInfoMap.put(attrKey, attrVal);
463: }
464: }
465: wcInfoMap.put(WebContainer.PORTAL_ID, sPortalID);
466:
467: try {
468: // create the web container instance
469: wcInstance = WebContainerFactory.getWebContainer(wcInfoMap,
470: false);
471: } catch (ConfigurationException ce) {
472: wcInstance = null;
473: throw new PSMBeanException("admin.error.create.wcinstance",
474: ce.getMessage(), ce);
475: }
476: }
477:
478: /* This method updates DEPLOYED_WEBAPPS instance attribute.
479: * If the operation is ADD_URI, uri is added to DEPLOYED_WEBAPPS.
480: * If the operation is REMOVE_URI, uri is deleted from DEPLOYED_WEBAPPS.
481: * This method was introduced to fix CR 6418165.
482: */
483: private synchronized void updateListOfDeployedWebapps(
484: PortalDomainContext pc, String uri, int operation)
485: throws Exception {
486: // Get the set of already deployed web application URIs
487: Set uriSet = pdc.getAttributeValues(TYPE, path,
488: InstanceAttributes.DEPLOYED_WEBAPPS);
489: if (uriSet.isEmpty()) {
490: uriSet = new HashSet();
491: }
492: switch (operation) {
493: case ADD_URI:
494: uriSet.add(uri);
495: break;
496: case REMOVE_URI:
497: uriSet.remove(uri);
498: break;
499: default:
500: // Don't update DEPLOYED_WEBAPPS
501: break;
502: }
503:
504: // Set the set of deployed URIs into the instance data
505: pdc.setAttributeValues(TYPE, path,
506: InstanceAttributes.DEPLOYED_WEBAPPS, uriSet);
507: }
508: }
|