0001: /*
0002: * BEGIN_HEADER - DO NOT EDIT
0003: *
0004: * The contents of this file are subject to the terms
0005: * of the Common Development and Distribution License
0006: * (the "License"). You may not use this file except
0007: * in compliance with the License.
0008: *
0009: * You can obtain a copy of the license at
0010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0011: * See the License for the specific language governing
0012: * permissions and limitations under the License.
0013: *
0014: * When distributing Covered Code, include this CDDL
0015: * HEADER in each file and include the License file at
0016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
0017: * If applicable add the following below this CDDL HEADER,
0018: * with the fields enclosed by brackets "[]" replaced with
0019: * your own identifying information: Portions Copyright
0020: * [year] [name of copyright owner]
0021: */
0022:
0023: /*
0024: * @(#)InstallationService.java
0025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
0026: *
0027: * END_HEADER - DO NOT EDIT
0028: */
0029: package com.sun.jbi.management.system;
0030:
0031: import com.sun.jbi.ComponentManager;
0032: import com.sun.jbi.ComponentInfo;
0033: import com.sun.jbi.ComponentType;
0034: import com.sun.jbi.ServiceUnitInfo;
0035: import com.sun.jbi.component.InstallationContext;
0036: import com.sun.jbi.management.ComponentInstallationContext;
0037: import com.sun.jbi.management.MBeanNames;
0038: import com.sun.jbi.management.LocalStringKeys;
0039: import com.sun.jbi.management.descriptor.ComponentDescriptor;
0040: import com.sun.jbi.management.descriptor.SharedLibraryDescriptor;
0041:
0042: import com.sun.jbi.management.internal.support.JarFactory;
0043: import com.sun.jbi.management.internal.support.XmlReader;
0044: import com.sun.jbi.management.message.MessageBuilder;
0045: import com.sun.jbi.management.support.JbiNameInfo;
0046: import com.sun.jbi.management.registry.Registry;
0047: import com.sun.jbi.management.registry.RegistryException;
0048: import com.sun.jbi.management.repository.RepositoryException;
0049: import com.sun.jbi.management.registry.GenericQuery;
0050: import com.sun.jbi.management.registry.Updater;
0051: import com.sun.jbi.management.repository.Archive;
0052: import com.sun.jbi.management.repository.ArchiveType;
0053: import com.sun.jbi.management.repository.Repository;
0054: import com.sun.jbi.management.support.MBeanHelper;
0055: import com.sun.jbi.management.util.FileHelper;
0056:
0057: import java.io.IOException;
0058: import java.io.File;
0059: import java.io.FileOutputStream;
0060: import java.io.InputStream;
0061: import java.math.BigInteger;
0062: import java.net.URL;
0063: import java.net.MalformedURLException;
0064: import java.util.jar.JarEntry;
0065: import java.util.jar.JarFile;
0066: import java.util.List;
0067: import java.util.logging.Level;
0068: import java.util.logging.Logger;
0069: import javax.management.ObjectName;
0070:
0071: import javax.jbi.JBIException;
0072: import javax.xml.bind.JAXBContext;
0073: import javax.xml.bind.Marshaller;
0074:
0075: /**
0076: * The installation service MBean allows administrative tools to manage
0077: * component and shared library installations. The tasks supported are:
0078: * <ul>
0079: * <li>Installing (and uninstalling) a shared library</li>
0080: * <li>Creating (loading) and destroying (unloading) a component installer
0081: * MBean.</li>
0082: * <li>Finding an existing component installer MBean.
0083: * </ul>
0084: *
0085: * Installing and uninstalling components is accomplished using
0086: * {@link InstallerMBean}s, loaded by this MBean. An individual installer MBean
0087: * is needed for each component installation / uninstallation. This is to support
0088: * the more complex installation process that some components require.
0089: *
0090: * @author JSR208 Expert Group
0091: */
0092: public class InstallationService extends ModelSystemService implements
0093: com.sun.jbi.management.InstallationServiceMBean {
0094: /**
0095: * path to the jbi.xml file
0096: */
0097: private static final String PATH_TO_JBI_XML_FILE = "META-INF/jbi.xml";
0098:
0099: /* Name of install root directory. */
0100: private static final String INSTALL_ROOT = "install_root";
0101:
0102: /** the workspace directory **/
0103: private static final String WORKSPACE = "workspace";
0104:
0105: /** the dir where component logger settings are stored **/
0106: private static final String LOGGER_CONFIG = "config";
0107:
0108: /** the delete me marker in repository **/
0109: private static final String DELETE_ME = ".DELETE_ME";
0110:
0111: /** the repository service assembly store **/
0112: private static final String REPOSITORY_SERVICE_ASSEMBLY_STORE = "service-assemblies";
0113:
0114: /** the tmp dir in JBI instance root**/
0115: private static final String TMP_DIR = "tmp";
0116:
0117: /** the tmp dir name used to extract the component to call framework validate for upgrade **/
0118: private static final String UPGRADE_TMP_DIR = "tmp_install_root";
0119:
0120: /** underscore used in dir names to save copies of install root during upgrade**/
0121: private static final String UNDERSCORE = "_";
0122:
0123: /** dir name used to save the SUs deployed to a component during component upgrade **/
0124: private static final String SU_BACKUP = "su";
0125:
0126: /**
0127: * JAR URL Protocol.
0128: */
0129: private static final String JAR = "jar";
0130:
0131: /**
0132: * FILE URL Protocol.
0133: */
0134: private static final String FILE = "file";
0135:
0136: /**
0137: * size of array, in bytes, to allocate
0138: */
0139: private static final int BYTE_ARRAY_SIZE = 4096;
0140:
0141: /** our immutable name: */
0142: private final JbiNameInfo mJbiNameInfo = new JbiNameInfo(
0143: "InstallationService");
0144:
0145: /**
0146: * Management context
0147: */
0148: private ManagementContext mContext = null;
0149:
0150: /**
0151: * Management Message Object
0152: */
0153: private BuildManagementMessageImpl mMMImpl = null;
0154:
0155: /**
0156: * Constructs a InstallationService.
0157: * @param anEnv is the ManagementContext.
0158: */
0159: public InstallationService(ManagementContext anEnv)
0160: throws javax.jbi.JBIException {
0161: mContext = anEnv;
0162:
0163: /*
0164: * Local initialization of this service.
0165: * Local routine is responsible for calling super.initModelSystemService(..).
0166: */
0167: initModelSystemService(anEnv.getEnvironmentContext());
0168:
0169: mMMImpl = mContext.getManagementMessageObject();
0170: }
0171:
0172: /**
0173: * local model init - called by constructor - create custom mbeans.
0174: * @param anEnv is the ManagementContext
0175: */
0176: protected void initModelSystemService(
0177: com.sun.jbi.EnvironmentContext anEnv) {
0178: String loggerName = com.sun.jbi.management.config.LoggerConfigurationFactory.INSTALLATION_LOGGER;
0179:
0180: Logger logger = Logger.getLogger(loggerName);
0181:
0182: //initialize the super.
0183: super .initModelSystemService(anEnv, logger, mJbiNameInfo);
0184:
0185: //add InstallationService MBean to START/STOP mbean set:
0186: mStartMBeans.add(mInstallServiceMBeanName,
0187: com.sun.jbi.management.InstallationServiceMBean.class,
0188: this );
0189: }
0190:
0191: /**
0192: * @param aJarFile jar file to be unjared.
0193: * @param aFileToFind String specifying which file to extract.
0194: * @return a File (created by CreateTempFile) with that one file.
0195: * @throws javax.jbi.JBIException if an error occurs
0196: */
0197: private File unjar(JarFile aJarFile, String aFileToFind)
0198: throws javax.jbi.JBIException {
0199: if (null == aJarFile) {
0200: String errMsg = mTranslator
0201: .getString(LocalStringKeys.IS_JAR_FILE_NULL);
0202: mLogger.warning(errMsg);
0203: String jbiTaskStr = managementMessageExcep("unjar",
0204: "IS_JAR_FILE_NULL", errMsg);
0205: throw new javax.jbi.JBIException(jbiTaskStr);
0206: }
0207:
0208: java.util.Enumeration entries = aJarFile.entries();
0209: byte[] buffer = new byte[BYTE_ARRAY_SIZE];
0210: while (entries.hasMoreElements()) {
0211: JarEntry entry = (JarEntry) entries.nextElement();
0212: FileOutputStream fos = null;
0213: InputStream is = null;
0214:
0215: if (!entry.isDirectory()
0216: && (entry.getName().equals(aFileToFind))) {
0217: try {
0218: File retFile = File.createTempFile("jbi", ".xml");
0219: fos = new FileOutputStream(retFile);
0220: is = aJarFile.getInputStream(entry);
0221: int len = is.read(buffer);
0222: while (0 < len) {
0223: fos.write(buffer, 0, len);
0224: len = is.read(buffer);
0225: }
0226: is.close();
0227: is = null;
0228: return retFile;
0229: } catch (java.io.IOException ioEx) {
0230: String errMsg = mTranslator.getString(
0231: LocalStringKeys.IS_IO_EXCEP, ioEx
0232: .getMessage());
0233: mLogger.warning(errMsg);
0234: String jbiTaskStr = managementMessageExcep("unjar",
0235: "IS_IO_EXCEP", errMsg, ioEx);
0236: throw new javax.jbi.JBIException(jbiTaskStr);
0237: } finally {
0238: try {
0239: if (null != is) {
0240: is.close();
0241: }
0242: if (null != fos) {
0243: fos.close();
0244: }
0245: } catch (java.io.IOException ioEx) {
0246: mLogger.finest(mTranslator.getString(
0247: LocalStringKeys.IS_IO_EXCEP, ioEx
0248: .getMessage()));
0249: }
0250: }
0251: }
0252: }
0253: return null;
0254: }
0255:
0256: /**
0257: * @param aJarFile jar file to be unjared.
0258: * @param aDirectory File (directory) where to unjar to.
0259: * @throws javax.jbi.JBIException if an error occurs
0260: */
0261: private void unjar(JarFile aJarFile, File aDirectory)
0262: throws javax.jbi.JBIException {
0263: java.util.Enumeration entries = aJarFile.entries();
0264: byte[] buffer = new byte[BYTE_ARRAY_SIZE];
0265: while (entries.hasMoreElements()) {
0266: JarEntry entry = (JarEntry) entries.nextElement();
0267: FileOutputStream fos = null;
0268: InputStream is = null;
0269:
0270: if (entry.isDirectory()) {
0271: File subdir = new File(aDirectory, entry.getName());
0272: if (!subdir.mkdir()) {
0273: String logMessage = mTranslator.getString(
0274: LocalStringKeys.IS_DEBUG_OUTPUT,
0275: "Could not create subdirectory "
0276: + entry.getName());
0277: mLogger.fine(logMessage);
0278: }
0279: } else {
0280: try {
0281: File newFile = new File(aDirectory, entry.getName());
0282: fos = new FileOutputStream(newFile);
0283: is = aJarFile.getInputStream(entry);
0284: int len = is.read(buffer);
0285: while (0 < len) {
0286: fos.write(buffer, 0, len);
0287: len = is.read(buffer);
0288: }
0289: is.close();
0290: is = null;
0291: } catch (java.io.IOException ioEx) {
0292: String errMsg = mTranslator.getString(
0293: LocalStringKeys.IS_IO_EXCEP, ioEx
0294: .getMessage());
0295: mLogger.warning(errMsg);
0296: String jbiTaskStr = managementMessageExcep("unjar",
0297: "IS_IO_EXCEP", errMsg, ioEx);
0298: throw new javax.jbi.JBIException(jbiTaskStr);
0299: } finally {
0300: try {
0301: if (null != is) {
0302: is.close();
0303: }
0304: if (null != fos) {
0305: fos.close();
0306: }
0307: } catch (java.io.IOException ioEx) {
0308: String errMsg = mTranslator.getString(
0309: LocalStringKeys.IS_IO_EXCEP, ioEx
0310: .getMessage());
0311: mLogger.fine(errMsg);
0312: }
0313: }
0314: }
0315: }
0316: }
0317:
0318: /* methods from installationServiceMBean */
0319:
0320: /**
0321: * Load the installer for a new component for the given component
0322: * installation package.
0323: *
0324: * @param installZipURL URL locating a ZIP file containing the
0325: * JBI Installation package to be installed; must be non-null,
0326: * non-empty, and a legal URL
0327: * @return the JMX ObjectName of the InstallerMBean loaded from
0328: * installZipURL; must be non-null
0329: */
0330: public synchronized ObjectName loadNewInstaller(String installZipURL) {
0331: ObjectName mbName;
0332:
0333: mLogger.finer(mTranslator.getString(
0334: LocalStringKeys.METHOD_ENTERED, "loadNewInstaller", "("
0335: + installZipURL + ")"));
0336:
0337: try {
0338: URL compURL = convertToProperURL(installZipURL);
0339:
0340: if (!mEnv.getPlatformContext().isAdminServer()) {
0341: /**
0342: * (1). Add the archive to the local registry / repository
0343: */
0344: File ar = new File(compURL.getFile());
0345: Archive compArchive = new Archive(ar, false);
0346: getRegistry().getRepository().addArchive(compArchive);
0347: getRegistry().getUpdater().addComponent(
0348: compArchive.getJbiName(),
0349: compArchive.getFileName(),
0350: compArchive.getUploadTimestamp());
0351: String path = compArchive.getPath();
0352: compURL = new File(path).toURL();
0353: ar.delete();
0354: }
0355:
0356: mbName = bootStrapComponent(compURL);
0357:
0358: mLogger.finer(mTranslator.getString(
0359: LocalStringKeys.METHOD_EXITED, "loadNewInstaller",
0360: "(" + installZipURL + ") -> " + mbName));
0361:
0362: return mbName;
0363: } catch (java.net.MalformedURLException URLsEx) {
0364: String errMsg = mTranslator.getString(
0365: LocalStringKeys.IS_URL_EXCEP, URLsEx.getMessage());
0366: mLogger.warning(errMsg);
0367: String jbiTaskStr = managementMessageExcep(
0368: "loadNewInstaller", "IS_URL_EXCEP", errMsg, URLsEx);
0369:
0370: throw new RuntimeException(jbiTaskStr);
0371: } catch (java.io.IOException ioex) {
0372: String errMsg = mTranslator.getString(
0373: LocalStringKeys.IS_IO_EXCEP, ioex.getMessage());
0374: mLogger.warning(errMsg);
0375: String jbiTaskStr = managementMessageExcep(
0376: "loadNewInstaller", "IS_IO_EXCEP", errMsg, ioex);
0377:
0378: throw new RuntimeException(jbiTaskStr);
0379: } catch (javax.jbi.JBIException jbiEx) {
0380: mLogger.warning(jbiEx.getMessage());
0381: throw new RuntimeException(jbiEx.getMessage());
0382: }
0383: }
0384:
0385: /**
0386: * Load the InstallerMBean for a previously installed component.
0387: * <p>
0388: * The "component name" refers to the
0389: * <code><identification><name></code> element value from the
0390: * component's installation package (see {@link #loadNewInstaller(String)}).
0391: *
0392: * @param componentName the component name identifying the installer to
0393: * load; must be non-null and non-empty
0394: * @return the JMX ObjectName of the InstallerMBean loaded from an existing
0395: * installation context; <code>null</code> if the installer MBean
0396: * doesn't exist
0397: */
0398: public synchronized ObjectName loadInstaller(String componentName) {
0399: ObjectName mbName = null;
0400:
0401: mLogger.finer(mTranslator.getString(
0402: LocalStringKeys.METHOD_ENTERED, "loadInstaller", "("
0403: + componentName + ")"));
0404:
0405: try {
0406: // If component is not installed on the instance return null.
0407: if (mContext.getComponentQuery().getComponentInfo(
0408: componentName) == null) {
0409: mLogger.warning(mTranslator.getString(
0410: LocalStringKeys.IS_COMPONENT_NOT_INSTALLED,
0411: componentName));
0412: } else {
0413: mbName = reloadInstaller(componentName);
0414: }
0415: mLogger.finer(mTranslator.getString(
0416: LocalStringKeys.METHOD_EXITED, "loadInstaller", "("
0417: + componentName + ") -> " + mbName));
0418: return mbName;
0419: } catch (javax.jbi.JBIException jbiEx) {
0420: mLogger.warning(jbiEx.getMessage());
0421: throw new RuntimeException(jbiEx.getMessage());
0422: }
0423: }
0424:
0425: /**
0426: * Unload an InstallerMBean previously loaded for a component.
0427: *
0428: * @param componentName the component name identifying the installer to
0429: * unload; must be non-null and non-empty
0430: * @param isToBeDeleted <code>true</code> if the component is to be deleted
0431: * as well
0432: * @return true if the operation was successful, otherwise false
0433: */
0434: public synchronized boolean unloadInstaller(String componentName,
0435: boolean isToBeDeleted) {
0436: ObjectName mbName;
0437:
0438: mLogger.finer(mTranslator.getString(
0439: LocalStringKeys.METHOD_ENTERED, "unloadInstaller", "("
0440: + componentName + "," + isToBeDeleted + ")"));
0441:
0442: try {
0443: // unload the loaded component bootstrap
0444: ComponentManager framework = mContext.getComponentManager();
0445: mLogger.fine("Unloading bootstrap for component "
0446: + componentName);
0447: framework.unloadBootstrap(componentName);
0448:
0449: if (isToBeDeleted
0450: && !mEnv.getPlatformContext().isAdminServer()) {
0451: // verify that the component has been uninstalled
0452: if (mContext.getComponentQuery().getComponentInfo(
0453: componentName) != null) {
0454: mLogger
0455: .warning(mTranslator
0456: .getString(
0457: LocalStringKeys.IS_COMPONENT_NOT_UNINSTALLED,
0458: componentName));
0459: return false;
0460: }
0461:
0462: getRegistry().getRepository().removeArchive(
0463: ArchiveType.COMPONENT, componentName);
0464: getRegistry().getUpdater().removeComponent("domain",
0465: componentName);
0466: }
0467: } catch (javax.jbi.JBIException jbiEx) {
0468: mLogger.warning(jbiEx.getMessage());
0469: throw new RuntimeException(jbiEx.getMessage());
0470: }
0471:
0472: mLogger.finer(mTranslator.getString(
0473: LocalStringKeys.METHOD_EXITED, "unloadInstaller", "("
0474: + componentName + "," + isToBeDeleted
0475: + ") -> TRUE"));
0476:
0477: return true;
0478: }
0479:
0480: /**
0481: * Install a shared library installation package.
0482: * <p>
0483: * The return value is the unique name for the shared-library, as found
0484: * in the the value of the installation descriptor's
0485: * <code><identification><name></code> element.
0486: *
0487: * @param slZipURL URL locating a zip file containing a shared library
0488: * installation package; must be non-null, non-empty, and a legal
0489: * URL
0490: * @return the unique name of the shared library loaded from slZipURL; must
0491: * be non-null and non-empty
0492: */
0493: public synchronized String installSharedLibrary(String slZipURL) {
0494: String slName;
0495:
0496: mLogger.finer(mTranslator.getString(
0497: LocalStringKeys.METHOD_ENTERED, "installSharedLibrary",
0498: "(" + slZipURL + ")"));
0499:
0500: try {
0501: URL theURL = convertToProperURL(slZipURL);
0502:
0503: if (!mEnv.getPlatformContext().isAdminServer()) {
0504: /**
0505: * (1). Add the archive to the local registry / repository
0506: */
0507: File ar = new File(theURL.getFile());
0508: Archive slArchive = new Archive(ar, false);
0509: getRegistry().getRepository().addArchive(slArchive);
0510: getRegistry().getUpdater().addSharedLibrary(
0511: slArchive.getJbiName(),
0512: slArchive.getFileName(),
0513: slArchive.getUploadTimestamp());
0514: String path = slArchive.getPath();
0515: theURL = new File(path).toURL();
0516: ar.delete();
0517:
0518: }
0519:
0520: // -- At this point the SharedLibrary install root is created
0521: // -- Just need to install
0522: slName = installSharedLibraryToFramework(theURL);
0523: mLogger.finer(mTranslator.getString(
0524: LocalStringKeys.METHOD_EXITED,
0525: "installSharedLibrary", "(" + slZipURL + ") -> "
0526: + slName));
0527:
0528: return slName;
0529: } catch (java.io.IOException URLsEx) {
0530: String errMsg = mTranslator.getString(
0531: LocalStringKeys.IS_URL_EXCEP, URLsEx.getMessage());
0532: mLogger.warning(errMsg);
0533: String jbiTaskStr = managementMessageExcep(
0534: "installSharedLibrary", "IS_URL_EXCEP", errMsg,
0535: URLsEx);
0536: throw new RuntimeException(jbiTaskStr);
0537: } catch (javax.jbi.JBIException jbiEx) {
0538: mLogger.warning(jbiEx.getMessage());
0539: throw new RuntimeException(jbiEx.getMessage());
0540: }
0541: }
0542:
0543: /**
0544: * Uninstall a previously installed shared library.
0545: *
0546: * @param slName the name of the shared name space to uninstall; must be
0547: * non-null and non-empty
0548: * @return true if the uninstall was successful
0549: */
0550: public synchronized boolean uninstallSharedLibrary(String slName) {
0551: Exception warning = null;
0552:
0553: mLogger.finer(mTranslator.getString(
0554: LocalStringKeys.METHOD_ENTERED,
0555: "uninstallSharedLibrary", "(" + slName + ")"));
0556:
0557: try {
0558: ComponentManager framework = mContext.getComponentManager();
0559: framework.uninstallSharedLibrary(slName);
0560: } catch (com.sun.jbi.framework.FrameworkWarningException fwEx) {
0561: warning = fwEx;
0562: // log this mLogger.warning below.
0563: } catch (javax.jbi.JBIException jbiEx) {
0564: mLogger.warning(jbiEx.getMessage());
0565: throw new RuntimeException(jbiEx.getMessage());
0566: // return false;
0567: }
0568:
0569: if (!mEnv.getPlatformContext().isAdminServer()) {
0570: /**
0571: * If it is not the DAS, then the shared library should be removed from the
0572: * local registry / repository
0573: *
0574: */
0575: try {
0576: getRegistry().getRepository().removeArchive(
0577: ArchiveType.SHARED_LIBRARY, slName);
0578: getRegistry().getUpdater().removeSharedLibrary(
0579: "domain", slName);
0580: } catch (Exception ex) {
0581: warning = ex;
0582: }
0583: }
0584:
0585: if (null != warning) {
0586: String message = warning.getMessage();
0587: String errMsg = mTranslator.getString(
0588: LocalStringKeys.WARNING, message);
0589: mLogger.warning(errMsg);
0590: ManagementMessageHolder mmHolder = new ManagementMessageHolder(
0591: "STATUS_MSG");
0592: mmHolder.setFrameworkLocale("en_US");
0593: mmHolder.setTaskName("uninstallSharedLibrary");
0594: mmHolder.setTaskResult("SUCCESS");
0595: mmHolder.setStatusMessageType("WARNING");
0596: mmHolder.setExceptionMessageType("WARNING");
0597: if ((message.length() > 11) && (message.startsWith("JBI"))) {
0598: mmHolder.setLocMessage(1, message.substring(11));
0599: mmHolder.setLocToken(1, message.substring(0, 9));
0600: } else {
0601: mmHolder.setLocMessage(1, message);
0602: mmHolder.setLocToken(1, "JBIFW1322");
0603: }
0604:
0605: String jbiTaskStr = mMMImpl
0606: .buildCompleteExceptionMessage(mmHolder);
0607: // this really should be a return value and NOT a thrown item.
0608: // but for EA5, uninstallSharedLibrary returns a boolean and not
0609: // a ManagementMessage. All the InstallationServiceMBean methods
0610: // really should RETURN that.
0611: throw new RuntimeException(jbiTaskStr);
0612: }
0613:
0614: return true;
0615: }
0616:
0617: /*---------------------------------------------------------------------------------*\
0618: * Extended InstallationServiceMBean ops *
0619: \*---------------------------------------------------------------------------------*/
0620:
0621: /**
0622: * Load the installer for a component from the Repository. The component archive from
0623: * the repository is uploaded to the target instance(s), loadNewInstaller() in
0624: * invoked on the remote instance(s). The object name of the Installer MBean
0625: * is returned.
0626: * <br/>
0627: * If the component is not there in the DAS repository a exception will
0628: * be thrown.
0629: *
0630: * @param componentName - name of the registered component.
0631: * @return the ObjectName of Installer MBean for the registered component
0632: * @exception javax.jbi.JBIException if the component is not registered.
0633: */
0634: public ObjectName loadInstallerFromRepository(String componentName)
0635: throws javax.jbi.JBIException {
0636: // nop
0637: return null;
0638: }
0639:
0640: /**
0641: * Install a shared library from the Repository. This method is not implemented and never
0642: * called by the facade MBeans.
0643: *
0644: * @param sharedLibraryName - name of the registered shared library.
0645: * @return the shared library name
0646: * @exception javax.jbi.JBIException if the shared library is not registered or
0647: * uninstall fails.
0648: */
0649: public String installSharedLibraryFromRepository(
0650: String sharedLibraryName) throws javax.jbi.JBIException {
0651: // nop
0652: return sharedLibraryName;
0653: }
0654:
0655: /**
0656: * Uninstall a previously installed shared library. The facade InstallationService
0657: * never invokes this operation.
0658: *
0659: * @param slName the name of the shared name space to uninstall; must be
0660: * non-null and non-empty
0661: * @param keep if true the shared libray is not deleted from the domain.
0662: * If false, the shared library is deleted from the repository
0663: * if after this uninstall it is not installed on any targets.
0664: * @return true if the uninstall was successful
0665: */
0666: public boolean uninstallSharedLibrary(String slName, boolean keep) {
0667: return uninstallSharedLibrary(slName);
0668: }
0669:
0670: /**
0671: * Upgrade a component. This is used to perform an upgrade of the runtime
0672: * files of a component without requiring undeployment of Service Assemblies
0673: * with Service Units deployed to the component.
0674: * @param componentName The name of the component.
0675: * @param installZipURL The URL to the component archive.
0676: * @return a status management message that contains component upgrade result
0677: * @throws JBIException if there is a problem with the upgrade.
0678: */
0679: public String upgradeComponent(String componentName,
0680: String installZipPath) throws javax.jbi.JBIException {
0681: doUpgradeComponent(componentName, installZipPath);
0682: return new MessageBuilder(mTranslator).buildFrameworkMessage(
0683: "upgradeComponent", MessageBuilder.TaskResult.SUCCESS);
0684: }
0685:
0686: /**
0687: * Set a component's upgrade number.
0688: * This method is used by synchronization to set the component upgrade number
0689: * @param componentName The name of the component.
0690: * @upgradeNumber the upgradeNumber.
0691: * @throws JBIException if there is a problem with the upgrade.
0692: */
0693: public void upgradeComponent(String componentName,
0694: long upgradeNumber) throws javax.jbi.JBIException {
0695: setComponentUpgradeNumber(componentName, upgradeNumber);
0696: }
0697:
0698: //Private methods
0699:
0700: /**
0701: * Reload the InstallerMBean for a previously installed component.
0702: *
0703: * @param aComponentId - the component UUID identifying the installer to load.
0704: * @return - the JMX ObjectName of the InstallerMBean loaded from an existing
0705: * installation context.
0706: * @throws javax.jbi.JBIException if an error occurs
0707: */
0708: private ObjectName reloadInstaller(String aComponentId)
0709: throws javax.jbi.JBIException {
0710: mLogger.fine(mTranslator.getString(
0711: LocalStringKeys.METHOD_ENTERED, "reloadInstaller", "("
0712: + aComponentId + ")"));
0713:
0714: MBeanNames jbiname = mContext.getMBeanNames();
0715: AdminService myAS = mContext.getAdminServiceHandle();
0716: XmlReader jbiXml;
0717: ObjectName returnName = null;
0718:
0719: String componentRoot = getInstallRoot(ArchiveType.COMPONENT,
0720: aComponentId);
0721:
0722: if (componentRoot != null) {
0723: mLogger.finest(mTranslator.getString(
0724: LocalStringKeys.IS_DEBUG_OUTPUT,
0725: "reloadInstaller: root = " + componentRoot));
0726:
0727: if (myAS.isEngine(aComponentId)) {
0728: returnName = jbiname.getEngineMBeanName(aComponentId,
0729: "Installer");
0730: if (mContext.getMBeanServer().isRegistered(returnName)) {
0731: mLogger.finest(mTranslator.getString(
0732: LocalStringKeys.IS_DEBUG_OUTPUT,
0733: "reloadInstaller: found existing engine InstallationMBean = "
0734: + returnName));
0735: return returnName;
0736: }
0737: } else if (myAS.isBinding(aComponentId)) {
0738: returnName = jbiname.getBindingMBeanName(aComponentId,
0739: "Installer");
0740: if (mContext.getMBeanServer().isRegistered(returnName)) {
0741: mLogger.finest(mTranslator.getString(
0742: LocalStringKeys.IS_DEBUG_OUTPUT,
0743: "reloadInstaller: found existing engine InstallationMBean = "
0744: + returnName));
0745: return returnName;
0746: }
0747: }
0748:
0749: jbiXml = getJbiXml(new File(componentRoot,
0750: PATH_TO_JBI_XML_FILE), false);
0751:
0752: ComponentManager framework = mContext.getComponentManager();
0753: try {
0754: ComponentInstallationContext cic = jbiXml
0755: .getInstallationContext();
0756: cic.setIsInstall(false);
0757: cic.setInstallRoot(componentRoot);
0758: mLogger.fine("Loading the bootstrap for component "
0759: + aComponentId);
0760: returnName = framework.loadBootstrap(cic, jbiXml
0761: .getBootstrapClassName(), jbiXml
0762: .getBootstrapClassPath(), jbiXml
0763: .getSharedLibraryIds());
0764: } catch (com.sun.jbi.framework.FrameworkWarningException fwEx) {
0765: // just log the mLogger.warning, but do not return it to the caller.
0766: mLogger.warning(fwEx.getMessage());
0767: } catch (javax.jbi.JBIException jbiEx) {
0768: String errMsg = mTranslator
0769: .getString(LocalStringKeys.IS_FRAMEWORK_UNINSTALL);
0770: String jbiTaskStr = managementMessageExcep("install",
0771: "IS_FRAMEWORK_UNINSTALL", jbiEx.getMessage(),
0772: jbiEx);
0773: throw new javax.jbi.JBIException(jbiTaskStr);
0774: }
0775: }
0776: return returnName;
0777: }
0778:
0779: /**
0780: * Eliminate spaces (-> "%20") to handle odd cases like installation
0781: * directories containing a space.
0782: *
0783: * @param installJarURI - String containing URL to convert
0784: * @return - a URL fixing spaces contained within.
0785: * @throws java.net.MalformedURLException if an error occurs
0786: */
0787: private URL convertToProperURL(String installJarURI)
0788: throws java.net.MalformedURLException {
0789: // fix problems commonly occuring with 'file:' URLs on Windows
0790: if (installJarURI.startsWith("file://")) {
0791: return new File(installJarURI.substring(7)).toURL();
0792: }
0793:
0794: if (installJarURI.startsWith("file:" + File.separator)) {
0795: return new File(installJarURI.substring(5)).toURL();
0796: }
0797:
0798: // next case: Windows only, skip over the /
0799: if (installJarURI.startsWith("file:/")) {
0800: return new File(installJarURI.substring(6)).toURL();
0801: }
0802:
0803: if (installJarURI.startsWith("file:")) {
0804: return new File(installJarURI.substring(5)).toURL();
0805: }
0806:
0807: // last ditch effort to handle URLs with non-encoded spaces
0808: try {
0809: return new java.net.URI(installJarURI
0810: .replaceAll(" ", "%20")).toURL();
0811: } catch (java.net.URISyntaxException e) {
0812: throw new java.net.MalformedURLException(e.getMessage());
0813: }
0814: }
0815:
0816: /**
0817: * Utility routine to insert a path prefix into a list of class paths
0818: * @param aPrefix - String containing the install root
0819: * @param aClassPath - List of class paths
0820: * @return List of class paths prefixed with the specified prefix
0821: */
0822: private List insertPathPrefix(String aPrefix, List aClassPath) {
0823: java.util.Vector retList = new java.util.Vector();
0824: for (int j = 0; j < aClassPath.size(); ++j) {
0825: String item = aPrefix + "/" + (String) aClassPath.get(j);
0826: retList.add(item);
0827: String logMessage = mTranslator.getString(
0828: LocalStringKeys.IS_DEBUG_OUTPUT,
0829: "insertPathPrefix: adding " + item);
0830: mLogger.finest(logMessage);
0831: if ("true"
0832: .equalsIgnoreCase(System
0833: .getProperty("com.sun.jbi.config.management.allowAbsolutePaths"))) {
0834: retList.add((String) aClassPath.get(j));
0835: logMessage = mTranslator.getString(
0836: LocalStringKeys.IS_DEBUG_OUTPUT,
0837: "insertPathPrefix: also adding "
0838: + aClassPath.get(j));
0839: mLogger.finest(logMessage);
0840: }
0841: }
0842: return retList;
0843: }
0844:
0845: /**
0846: * Utility routine to remove a directory or file.
0847: * @return 0 for success.
0848: * @param aFileOrDir file/directory to be removed.
0849: */
0850: private int removeIt(String aFileOrDir) {
0851: int st = 0;
0852: java.io.File f = new java.io.File(aFileOrDir);
0853: if (f.exists() && !f.delete()) {
0854: f.deleteOnExit();
0855: st++;
0856: }
0857:
0858: return st;
0859: }
0860:
0861: /**
0862: * Utility routine to recursively remove a directory.
0863: * @return 0 if directory is successfully removed (recursively).
0864: * @param aDir directory name.
0865: */
0866: private int removeDirectory(String aDir) {
0867: int st = 0;
0868: String filePath;
0869: java.io.File d = new java.io.File(aDir);
0870: if (!d.exists()) {
0871: return st;
0872: }
0873:
0874: if (!d.isDirectory()) {
0875: return removeIt(aDir);
0876: }
0877:
0878: String[] filesToBeRemoved = d.list();
0879:
0880: for (int i = 0; i < filesToBeRemoved.length; i++) {
0881: filePath = aDir + "/" + filesToBeRemoved[i];
0882: java.io.File f = new java.io.File(filePath);
0883: if (f.isDirectory()) {
0884: st += removeDirectory(filePath);
0885: } else {
0886: st += removeIt(filePath);
0887: }
0888: }
0889: st += removeIt(aDir);
0890: return st;
0891: }
0892:
0893: /**
0894: * Utility routine to move an unable-to-delete directory
0895: * into a "Trash" directory.
0896: * @param aDir directory name.
0897: */
0898: private void moveToTrash(String aDir) {
0899: String logMessage = mTranslator.getString(
0900: LocalStringKeys.METHOD_ENTERED, "moveToTrash", "("
0901: + aDir + ")");
0902: mLogger.fine(logMessage);
0903:
0904: String jbiRoot = this .getJbiInstallRoot();
0905:
0906: if ((null == jbiRoot) || ("".equals(jbiRoot))) {
0907: logMessage = mTranslator
0908: .getString(LocalStringKeys.IS_NO_TRASH);
0909: mLogger.warning(logMessage);
0910: return;
0911: }
0912:
0913: jbiRoot += "/Trash";
0914: int dirNum = 1;
0915: String trashRootPath = jbiRoot + "/" + dirNum;
0916: File trashRoot = new File(trashRootPath);
0917: while (trashRoot.exists()) {
0918: dirNum++;
0919: trashRootPath = jbiRoot + "/" + dirNum;
0920: trashRoot = new File(trashRootPath);
0921: }
0922: logMessage = mTranslator.getString(
0923: LocalStringKeys.IS_DEBUG_OUTPUT,
0924: "Creating Trash directory as " + trashRootPath);
0925: mLogger.fine(logMessage);
0926: if (!trashRoot.mkdirs()) {
0927: logMessage = mTranslator.getString(
0928: LocalStringKeys.IS_DEBUG_OUTPUT,
0929: "Cannot create directory " + trashRootPath);
0930: mLogger.warning(logMessage);
0931: return;
0932: }
0933: logMessage = mTranslator.getString(
0934: LocalStringKeys.IS_DEBUG_OUTPUT, "Now about to rename "
0935: + aDir + " to " + trashRootPath + ".");
0936: mLogger.fine(logMessage);
0937: File oldRoot = new File(aDir);
0938: if (!oldRoot.renameTo(trashRoot)) {
0939: logMessage = mTranslator.getString(
0940: LocalStringKeys.IS_DEBUG_OUTPUT,
0941: "Could not rename " + aDir + " to " + trashRootPath
0942: + ".");
0943: mLogger.warning(logMessage);
0944: }
0945: }
0946:
0947: /**
0948: * Return the InstallerMBean for a component.
0949: *
0950: * @param aComponentId - the component UUID identifying the component.
0951: * @return - the JMX ObjectName of the InstallerMBean for that component.
0952: */
0953: private ObjectName installerMBeanName(String aComponentId) {
0954: mLogger.fine(mTranslator.getString(
0955: LocalStringKeys.METHOD_ENTERED, "installerMBeanName",
0956: "(" + aComponentId + ")"));
0957:
0958: ObjectName mbName;
0959: AdminService admin;
0960: ComponentInfo compInfo;
0961:
0962: admin = mContext.getAdminServiceHandle();
0963: compInfo = mContext.getComponentQuery().getComponentInfo(
0964: aComponentId);
0965:
0966: if (compInfo == null) {
0967: // framework does not know about this component
0968: mbName = null;
0969: } else if (compInfo.getComponentType() == ComponentType.ENGINE) {
0970: mbName = mContext.getMBeanNames().getEngineMBeanName(
0971: aComponentId, "Installer");
0972: } else if (compInfo.getComponentType() == ComponentType.BINDING) {
0973: mbName = mContext.getMBeanNames().getBindingMBeanName(
0974: aComponentId, "Installer");
0975: } else {
0976: mLogger.warning(mTranslator.getString(
0977: LocalStringKeys.IS_COMPONENT_NO_INSTALL_MBEAN,
0978: aComponentId));
0979: mbName = null;
0980: }
0981:
0982: mLogger.finer(mTranslator.getString(
0983: LocalStringKeys.METHOD_EXITED, "installerMBeanName",
0984: "(" + aComponentId + ") -> " + mbName));
0985:
0986: return mbName;
0987: }
0988:
0989: /**
0990: * Return the install root prefix.
0991: *
0992: * @return - the base component root directory.
0993: */
0994: private String getInstallRootPrefix() {
0995: String jbiRoot = mContext.getJbiInstallRoot();
0996:
0997: if ((null == jbiRoot) || ("".equals(jbiRoot))) {
0998: jbiRoot = System.getProperty("junit.srcroot")
0999: + "/install/lassen";
1000: }
1001: return jbiRoot;
1002: }
1003:
1004: /**
1005: * Obtain the JBI install root, either from the environment
1006: * context, or from a property.
1007: * @return - the component root prefix.
1008: */
1009: private String getJbiInstallRoot() {
1010: String jbiRoot = mContext.getJbiInstallRoot();
1011:
1012: mLogger.finest(mTranslator.getString(
1013: LocalStringKeys.IS_DEBUG_OUTPUT,
1014: "getJbiInstallRoot: jbiRoot = " + jbiRoot));
1015:
1016: if ((null == jbiRoot) || ("".equals(jbiRoot))) {
1017: jbiRoot = System.getProperty("junit.srcroot")
1018: + "/install/lassen";
1019: }
1020: jbiRoot = jbiRoot.replace('\\', '/');
1021: mLogger.finer(mTranslator.getString(
1022: LocalStringKeys.METHOD_EXITED, "getJbiInstallRoot",
1023: "() -> " + jbiRoot));
1024:
1025: return jbiRoot;
1026: }
1027:
1028: /**
1029: * Close the given (open) JarFile, ignoring IOExceptions
1030: * @param aJarFile - an open Jar file
1031: */
1032: void closeJarFile(JarFile aJarFile) {
1033: try {
1034: aJarFile.close();
1035: } catch (java.io.IOException ioEx) {
1036: String errMsg = mTranslator.getString(
1037: LocalStringKeys.IS_IO_EXCEP, ioEx.getMessage());
1038: mLogger.finest(errMsg);
1039: }
1040: }
1041:
1042: /**
1043: * Helper method to create a ManagementMessage exception
1044: * @param methodName - the method generating the exception
1045: * @param token - the message key for the exception
1046: * @param message - the message itself
1047: */
1048: private String managementMessageExcep(String methodName,
1049: String token, String message) {
1050: ManagementMessageHolder mmHolder = new ManagementMessageHolder(
1051: "EXCEPTION_MSG");
1052: mmHolder.setFrameworkLocale("en_US");
1053: mmHolder.setTaskName(methodName);
1054: mmHolder.setTaskResult("FAILED");
1055: if ((message.length() > 11) && (message.startsWith("JBI"))) {
1056: mmHolder.setLocMessage(1, message.substring(11));
1057: mmHolder.setLocToken(1, message.substring(0, 9));
1058: } else {
1059: mmHolder.setLocMessage(1, message);
1060: mmHolder.setLocToken(1, token);
1061: }
1062: mmHolder.setExceptionMessageType("ERROR");
1063: mmHolder.setFrameworkLocale("en_US");
1064: String jbiTaskStr = mMMImpl
1065: .buildCompleteExceptionMessage(mmHolder);
1066: return jbiTaskStr;
1067: }
1068:
1069: /**
1070: * Helper method to create a ManagementMessage exception
1071: * @param methodName - the method generating the exception
1072: * @param token - the message key for the exception
1073: * @param excep - the exception itself (to wrapper)
1074: */
1075: private String managementMessageExcep(String methodName,
1076: String token, String message, Throwable excep) {
1077: ManagementMessageHolder mmHolder = new ManagementMessageHolder(
1078: "EXCEPTION_MSG");
1079: mmHolder.setFrameworkLocale("en_US");
1080: mmHolder.setTaskName(methodName);
1081: mmHolder.setTaskResult("FAILED");
1082: mmHolder.setExceptionObject(excep);
1083: if ((message.length() > 11) && (message.startsWith("JBI"))) {
1084: mmHolder.setLocMessage(1, message.substring(11));
1085: mmHolder.setLocToken(1, message.substring(0, 9));
1086: } else {
1087: mmHolder.setLocMessage(1, message);
1088: mmHolder.setLocToken(1, token);
1089: }
1090: mmHolder.setExceptionMessageType("ERROR");
1091: mmHolder.setFrameworkLocale("en_US");
1092: String jbiTaskStr = mMMImpl
1093: .buildCompleteExceptionMessage(mmHolder);
1094: return jbiTaskStr;
1095: }
1096:
1097: /** Returns an XmlReader representation of the jbi.xml descriptor in
1098: * the specified JAR. If an error occurs, the JAR is closed before
1099: * throwing an exception.
1100: */
1101: private XmlReader getJbiXml(JarFile jar)
1102: throws javax.jbi.JBIException {
1103: File jbiFile;
1104:
1105: try {
1106: jbiFile = unjar(jar, PATH_TO_JBI_XML_FILE);
1107: return getJbiXml(jbiFile, true);
1108: } catch (javax.jbi.JBIException jbiEx) {
1109: closeJarFile(jar);
1110: throw jbiEx;
1111: }
1112: }
1113:
1114: /** Returns an XmlReader representation of the jbi.xml descriptor in
1115: * the specified JAR. If an error occurs, the JAR is closed before
1116: * throwing an exception.
1117: */
1118: private XmlReader getJbiXml(File jbiFile, boolean validate)
1119: throws javax.jbi.JBIException {
1120: XmlReader jbiXml;
1121: if (null == jbiFile) {
1122: String errMsg = mTranslator
1123: .getString(LocalStringKeys.IS_NO_JBI_XML_IN_JAR);
1124: mLogger.warning(errMsg);
1125: String jbiTaskStr = managementMessageExcep("install",
1126: "IS_NO_JBI_XML_IN_JAR", errMsg);
1127: throw new javax.jbi.JBIException(jbiTaskStr);
1128: }
1129:
1130: try {
1131: jbiXml = new XmlReader();
1132: jbiXml.setEnvironmentContext(mContext
1133: .getEnvironmentContext());
1134:
1135: jbiXml.loadAndParse(jbiFile.getAbsolutePath(), true);
1136:
1137: return jbiXml;
1138: } catch (javax.jbi.JBIException jbiEx) {
1139: throw (jbiEx);
1140: } catch (Exception e) {
1141: mLogger.warning(e.getMessage());
1142: String errMsg = mTranslator
1143: .getString(LocalStringKeys.IS_VALIDATION_EXCEP, e
1144: .getMessage());
1145: String jbiTaskStr = managementMessageExcep("install",
1146: "IS_VALIDATION_EXCEP", errMsg, e);
1147: throw new javax.jbi.JBIException(jbiTaskStr);
1148: }
1149: }
1150:
1151: /*---------------------------------------------------------------------------------*\
1152: * Private Helpers *
1153: \*---------------------------------------------------------------------------------*/
1154:
1155: /**
1156: * @param alURL - path to the shared library archive
1157: */
1158: String installSharedLibraryToFramework(URL aComponentURL)
1159: throws javax.jbi.JBIException {
1160: Archive installArchive;
1161: String slName;
1162:
1163: mLogger.fine(mTranslator.getString(
1164: LocalStringKeys.METHOD_ENTERED, "install", "("
1165: + aComponentURL + ")"));
1166:
1167: installArchive = getRepositoryArchive(aComponentURL);
1168:
1169: slName = installArchive.getJbiName();
1170:
1171: String slInstallRoot = getInstallRoot(
1172: ArchiveType.SHARED_LIBRARY, slName);
1173:
1174: mLogger.finer(mTranslator.getString(
1175: LocalStringKeys.IS_DEBUG_OUTPUT, "install Root = "
1176: + slInstallRoot));
1177:
1178: mLogger.finer(mTranslator.getString(
1179: LocalStringKeys.IS_DEBUG_OUTPUT,
1180: "Installing shared library to framework"));
1181: ComponentManager framework = mContext.getComponentManager();
1182:
1183: SharedLibraryDescriptor descr = new SharedLibraryDescriptor(
1184: installArchive.getJbiXml(false));
1185:
1186: try {
1187: framework
1188: .installSharedLibrary(
1189: slName,
1190: "The description is not persisted, should be removed.",
1191: slInstallRoot,
1192: descr.isSharedLibraryClassLoaderSelfFirst(),
1193: insertPathPrefix(
1194: slInstallRoot,
1195: descr
1196: .getSharedLibraryClassPathElements()));
1197:
1198: mLogger.finest(mTranslator.getString(
1199: LocalStringKeys.IS_DEBUG_OUTPUT,
1200: "installed shared library = " + slName));
1201:
1202: } catch (javax.jbi.JBIException jbiEx) {
1203: String errMsg = mTranslator
1204: .getString(LocalStringKeys.IS_FRAMEWORK_INSTALL);
1205: String jbiTaskStr = managementMessageExcep("install",
1206: "IS_FRAMEWORK_INSTALL", jbiEx.getMessage(), jbiEx);
1207: throw new javax.jbi.JBIException(jbiTaskStr);
1208: }
1209:
1210: return slName;
1211: }
1212:
1213: /**
1214: * Create the Component Installation Context and load the bootstrap.
1215: *
1216: * @param aComponentURL the component .jar specification
1217: * @param isSharedLib - true iff the component is a shared library.
1218: * @return the ID of the component/library
1219: * @throws javax.jbi.JBIException if an error occurs
1220: */
1221: ObjectName bootStrapComponent(URL aComponentURL)
1222: throws javax.jbi.JBIException {
1223: Archive installArchive;
1224: String compName;
1225: ObjectName mbName;
1226:
1227: mLogger.fine(mTranslator.getString(
1228: LocalStringKeys.METHOD_ENTERED, "bootStrapComponent",
1229: "(" + aComponentURL + ")"));
1230:
1231: installArchive = getRepositoryArchive(aComponentURL);
1232:
1233: compName = installArchive.getJbiName();
1234:
1235: // -- Create installation Context
1236: String compInstallRoot = getInstallRoot(ArchiveType.COMPONENT,
1237: compName);
1238: mLogger.finer(mTranslator.getString(
1239: LocalStringKeys.IS_DEBUG_OUTPUT, "install Root = "
1240: + compInstallRoot));
1241:
1242: mLogger.finer(mTranslator.getString(
1243: LocalStringKeys.IS_DEBUG_OUTPUT,
1244: "Loading the Component Bootstrap"));
1245: ComponentManager framework = mContext.getComponentManager();
1246: try {
1247: // -- Create the Component Workspace
1248: String workspaceRoot = compInstallRoot + File.separator
1249: + "workspace";
1250: new File(workspaceRoot).mkdirs();
1251:
1252: ComponentDescriptor descr = new ComponentDescriptor(
1253: installArchive.getJbiXml(false));
1254:
1255: // -- Component Installation Context
1256: ComponentInstallationContext cic = getComponentInstallationContext(
1257: descr, compInstallRoot);
1258: cic.setIsInstall(true);
1259: cic.setInstallRoot(compInstallRoot);
1260: cic.setWorkspaceRoot(workspaceRoot);
1261:
1262: // -- Load Bootstrap
1263: mbName = framework.loadBootstrap(cic, descr
1264: .getBootstrapClassName(), insertPathPrefix(
1265: compInstallRoot, descr
1266: .getBootstrapClassPathElements()), descr
1267: .getSharedLibraryIds());
1268:
1269: mLogger.finest(mTranslator.getString(
1270: LocalStringKeys.IS_DEBUG_OUTPUT,
1271: "loadBootstrap MBean = " + mbName));
1272:
1273: } catch (javax.jbi.JBIException jbiEx) {
1274: String errMsg = mTranslator
1275: .getString(LocalStringKeys.IS_FRAMEWORK_INSTALL);
1276: String jbiTaskStr = managementMessageExcep(
1277: "loadNewInstaller", "IS_FRAMEWORK_INSTALL", jbiEx
1278: .getMessage(), jbiEx);
1279: throw new javax.jbi.JBIException(jbiTaskStr);
1280: }
1281:
1282: return mbName;
1283: }
1284:
1285: /**
1286: * @return the shared library root
1287: */
1288: private String getInstallRoot(ArchiveType type, String name)
1289: throws javax.jbi.JBIException {
1290: Archive archive = null;
1291: String archivePath;
1292: String installRoot = null;
1293:
1294: archive = getRegistry().getRepository().getArchive(type, name);
1295:
1296: if (archive != null) {
1297: archivePath = archive.getPath();
1298: installRoot = new File(archivePath).getParent()
1299: + File.separator + INSTALL_ROOT;
1300: }
1301:
1302: mLogger.finest("Install Root for component " + name + " is "
1303: + installRoot);
1304: return installRoot;
1305: }
1306:
1307: private Archive getRepositoryArchive(URL aURL)
1308: throws javax.jbi.JBIException {
1309: try {
1310: return new Archive(new File(aURL.getFile()), false);
1311: } catch (Exception jbiEx) {
1312: String errMsg = mTranslator
1313: .getString(LocalStringKeys.IS_FRAMEWORK_INSTALL);
1314: String jbiTaskStr = managementMessageExcep(
1315: "getRepositoryArchive", "IS_FRAMEWORK_INSTALL",
1316: jbiEx.getMessage(), jbiEx);
1317: throw new javax.jbi.JBIException(jbiTaskStr);
1318: }
1319: }
1320:
1321: private ComponentInstallationContext getComponentInstallationContext(
1322: ComponentDescriptor descr, String installRoot)
1323: throws javax.jbi.JBIException {
1324:
1325: String componentClassName = descr.getComponentClassName();
1326: String componentName = descr.getName();
1327:
1328: boolean isBinding = (descr.getComponentType() == com.sun.jbi.ComponentType.BINDING);
1329: org.w3c.dom.DocumentFragment docFrag = null;
1330:
1331: try {
1332: //docFrag = getExtensionData(jbi);
1333: docFrag = getExtensionData(installRoot);
1334: } catch (Exception ex) {
1335: throw new javax.jbi.JBIException(ex);
1336: }
1337:
1338: ComponentInstallationContext ic = new ComponentInstallationContext(
1339: componentName, (isBinding ? InstallationContext.BINDING
1340: : InstallationContext.ENGINE),
1341: componentClassName, descr
1342: .getComponentClassPathElements(), docFrag);
1343: ic.setDescription(descr.getDescription());
1344: if (descr.isBootstrapClassLoaderSelfFirst()) {
1345: ic.setBootstrapClassLoaderSelfFirst();
1346: }
1347: if (descr.isComponentClassLoaderSelfFirst()) {
1348: ic.setComponentClassLoaderSelfFirst();
1349: }
1350: return ic;
1351: }
1352:
1353: /**
1354: * Trim the whitespaces from all the elements in the list.
1355: */
1356: private List<String> trim(List<String> list) {
1357: List<String> trimmedList = new java.util.ArrayList<String>(list
1358: .size());
1359: for (String str : list) {
1360: trimmedList.add(str.trim());
1361: }
1362: return trimmedList;
1363: }
1364:
1365: /**
1366: * Get the extension data from the Components jbi.xml and return the
1367: * data in a DocumentFragment
1368: */
1369: private org.w3c.dom.DocumentFragment getExtensionData(
1370: String componentRoot) throws Exception {
1371: /**
1372: com.sun.jbi.management.util.ComponentType compType = jbi.getComponent();
1373: Marshaller m = sJC.createMarshaller();
1374:
1375: javax.xml.parsers.DocumentBuilderFactory
1376: dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
1377: dbf.setNamespaceAware(true);
1378: javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
1379: org.w3c.dom.DocumentFragment
1380: docFrag = db.newDocument().createDocumentFragment();
1381:
1382: List<Object> anyData = compType.getAnyOrAny();
1383:
1384: for ( Object extObj : anyData )
1385: {
1386: m.marshal(extObj, docFrag);
1387: }
1388:
1389: return docFrag;
1390: */
1391:
1392: // have to do it this way there is a bug in JAXB getAnyOrAny
1393: // does not work correctly
1394: XmlReader jbiXml = getJbiXml(new File(componentRoot,
1395: PATH_TO_JBI_XML_FILE), false);
1396: ComponentInstallationContext cic = jbiXml
1397: .getInstallationContext();
1398:
1399: return cic.getInstallationDescriptorExtension();
1400: }
1401:
1402: /**
1403: * This method is used to extract a given jar in the specified dir.
1404: * @param parentDir the dir to extract the jar in
1405: * @param archiveZip the jar file
1406: * @throws IOException if there are problems in extracting the jar
1407: */
1408: private void extractJar(String parentDir, File archiveZip)
1409: throws IOException {
1410:
1411: File parent = new File(parentDir);
1412: parent.mkdir();
1413: JarFactory jarHelper = new JarFactory(parent.getAbsolutePath());
1414: jarHelper.unJar(archiveZip);
1415: mLogger.finer("Extracted " + archiveZip.getName() + " in "
1416: + parentDir);
1417:
1418: }
1419:
1420: /**
1421: * This method is used to validate the component for upgrade.
1422: * This method creates a temporary install root for the component and
1423: * calls framework's validate using the temporary install root.
1424: * @param componentName the componentName
1425: * @param installZipURL the path to the component file given for upgrade
1426: * @return true if the component involved implements upgrade SPI
1427: * @throws JBIException if the component is not validated for upgrade
1428: */
1429: private boolean validateComponentForUpgrade(String componentName,
1430: String installZipURL) throws JBIException {
1431: boolean isUpgradeImplemented = false;
1432: String tempInstallRoot = mEnv.getJbiInstanceRoot()
1433: + File.separator + TMP_DIR + File.separator
1434: + componentName + UPGRADE_TMP_DIR
1435: + FileHelper.getTimestamp();
1436: try {
1437: File updateFile = new File(installZipURL);
1438:
1439: extractJar(tempInstallRoot, updateFile);
1440: Archive updateArchive = new Archive(updateFile, true);
1441:
1442: ComponentDescriptor descriptor = new ComponentDescriptor(
1443: updateArchive.getJbiXml(false));
1444: ComponentInstallationContext installationContext = getComponentInstallationContext(
1445: descriptor, tempInstallRoot);
1446: installationContext.setIsInstall(false);
1447: installationContext.setInstallRoot(tempInstallRoot);
1448:
1449: isUpgradeImplemented = mEnv
1450: .getComponentManager()
1451: .validateComponentForUpgrade(
1452: installationContext,
1453: descriptor.getBootstrapClassName(),
1454: insertPathPrefix(
1455: tempInstallRoot,
1456: descriptor
1457: .getBootstrapClassPathElements()),
1458: descriptor.getSharedLibraryIds());
1459:
1460: mLogger.fine("Framework has validated component archive "
1461: + installZipURL);
1462:
1463: File tempInstallRootFile = new File(tempInstallRoot);
1464: FileHelper.cleanDirectory(tempInstallRootFile);
1465: tempInstallRootFile.delete();
1466: } catch (IOException ioEx) {
1467: if (tempInstallRoot != null) {
1468: File tempInstallRootFile = new File(tempInstallRoot);
1469: if (tempInstallRootFile.exists()) {
1470: FileHelper.cleanDirectory(tempInstallRootFile);
1471: tempInstallRootFile.delete();
1472: }
1473: }
1474: throw new JBIException(ioEx.toString());
1475: }
1476: return isUpgradeImplemented;
1477: }
1478:
1479: /**
1480: * This method is used to backup existing install root for a component
1481: * @param installRoot the installRoot
1482: * @param componentName the component name
1483: * @return String the backupDir where the installroot was saved
1484: * @throws IOException if there are issues in creating a backup of the install root
1485: * @throws RegistryException if information about the component could not be read
1486: * from registry
1487: */
1488: private String backupInstallRoot(String installRoot,
1489: String componentName) throws IOException, RegistryException {
1490: String installRootBkup = mEnv.getJbiInstanceRoot()
1491: + File.separator
1492: + TMP_DIR
1493: + File.separator
1494: + componentName
1495: + UNDERSCORE
1496: + getRegistry().getGenericQuery()
1497: .getComponentUpgradeNumber(componentName)
1498: + UNDERSCORE + FileHelper.getTimestamp();
1499: FileHelper.copy(installRoot, installRootBkup);
1500: mLogger.finer("Install root has been saved in "
1501: + installRootBkup);
1502: return installRootBkup;
1503: }
1504:
1505: /**
1506: * This method is used to upgrade a component by calling framework's
1507: * upgrade component
1508: * @param componentName the component name
1509: * @throws JBIException if the component could not be upgraded
1510: */
1511: private void invokeFrameworkUpgradeComponent(String componentName)
1512: throws JBIException {
1513: Archive archive = getRegistry().getRepository().getArchive(
1514: ArchiveType.COMPONENT, componentName);
1515: String compInstallRoot = getInstallRoot(ArchiveType.COMPONENT,
1516: componentName);
1517: ComponentManager framework = mContext.getComponentManager();
1518: ComponentDescriptor descr = new ComponentDescriptor(archive
1519: .getJbiXml(false));
1520:
1521: ComponentInstallationContext installationContext = getComponentInstallationContext(
1522: descr, compInstallRoot);
1523: installationContext.setIsInstall(true);
1524: installationContext.setInstallRoot(compInstallRoot);
1525: installationContext.setWorkspaceRoot(compInstallRoot
1526: + File.separator + WORKSPACE);
1527:
1528: framework.upgradeComponent(installationContext, descr
1529: .getBootstrapClassName(),
1530: insertPathPrefix(compInstallRoot, descr
1531: .getBootstrapClassPathElements()), descr
1532: .getSharedLibraryIds());
1533: mLogger.finer("framework successfully upgraded the component "
1534: + componentName);
1535: }
1536:
1537: /**
1538: * This method is used to copy the workspace of a component from the backup
1539: * dir to the newly created install root
1540: * @param backupDir the dir where the component install root was saved
1541: * @param componentName component name
1542: * @param componentRoot the install root of the component
1543: * @throws IOException if the workspace dir could not be recreated
1544: */
1545: private void copyWorkspaceFromBackup(String backupDir,
1546: String componentName, String componentRoot)
1547: throws IOException {
1548: /*
1549: * backupDir is <instanceRoot>/tmp/sun-http-binding_backup
1550: * installRoot is <instanceRoot>/components/sun-http-binding
1551: * we need to copy backupDir/intall_root/workspace to
1552: * components/sun-http-binding/install_root/workspace
1553: */
1554: String backupWorkspace = backupDir + File.separator
1555: + INSTALL_ROOT + File.separator + WORKSPACE;
1556: String workspace = componentRoot + File.separator
1557: + INSTALL_ROOT + File.separator + WORKSPACE;
1558: FileHelper.copy(backupWorkspace, workspace);
1559: mLogger.fine("Recreated component workspace from "
1560: + backupWorkspace + " to " + workspace);
1561:
1562: }
1563:
1564: /**
1565: * This method is used to copy logger settings of a omponent from the backup
1566: * dir to the newly created install root
1567: * @param backupDir the dir where the component install root was saved
1568: * @param componentName component name
1569: * @param componentRoot the install root of the component
1570: * @throws IOException if the workspace dir could not be recreated
1571: */
1572: private void copyLoggerSettingsFromBackup(String backupDir,
1573: String componentName, String componentRoot)
1574: throws IOException {
1575: /*
1576: * backupDir is <instanceRoot>/tmp/sun-http-binding_backup
1577: * installRoot is <instanceRoot>/components/sun-http-binding
1578: * we need to copy backupDir/intall_root/workspace to
1579: * components/sun-http-binding/install_root/workspace
1580: */
1581: String backupConfig = backupDir + File.separator
1582: + LOGGER_CONFIG;
1583: String config = componentRoot + File.separator + LOGGER_CONFIG;
1584: boolean result = FileHelper.copy(backupConfig, config);
1585: if (result) {
1586: mLogger.fine("Recreated component logger settings from "
1587: + backupConfig + " to " + config);
1588: } else {
1589: if (new File(backupConfig).exists()) {
1590: mLogger
1591: .warning(mTranslator
1592: .getString(
1593: LocalStringKeys.IS_COMPONENT_UPGRADE_LOGGER_NOT_RESTORED,
1594: componentName));
1595: }
1596: }
1597:
1598: }
1599:
1600: /**
1601: * This method is used to restore a component's install root from a backup
1602: * copy. This method is used in cases where there were issues in component
1603: * upgrade and we need to restore the component install root
1604: * @param componentName the component name
1605: * @param backupDir the dir where the component install root was saved
1606: * @param componentRoot the install root for the component
1607: */
1608: private void restoreInstallRoot(String componentName,
1609: String backupComponentRoot, String componentRoot) {
1610: /*
1611: * backupDir is <instanceRoot>/tmp/sun-http-binding_backup
1612: * installRoot is <instanceRoot>/components/sun-http-binding
1613: * we need to copy backupDir to <instanceRoot>/components
1614: */
1615: try {
1616: if (getRegistry().getRepository().archiveExists(
1617: ArchiveType.COMPONENT, componentName)) {
1618:
1619: getRegistry().getRepository().removeArchive(
1620: ArchiveType.COMPONENT, componentName);
1621: }
1622: //remove the marker
1623: File marker = new File(componentRoot, DELETE_ME);
1624: if (marker != null && marker.exists()) {
1625: marker.delete();
1626: }
1627:
1628: FileHelper.copy(backupComponentRoot, componentRoot);
1629:
1630: File backupDir = new File(backupComponentRoot);
1631: FileHelper.cleanDirectory(backupDir);
1632: backupDir.delete();
1633: mLogger.fine("Restored component install root from "
1634: + backupComponentRoot);
1635: } catch (Exception ex) {
1636: mLogger
1637: .warning(mTranslator
1638: .getString(LocalStringKeys.IS_COMPONENT_UPGRADE_INSTALL_ROOT_RESTORE_FAILED)
1639: + ex.getMessage());
1640: }
1641:
1642: }
1643:
1644: /**
1645: * Actually upgrade a component. This is used to perform an upgrade of the runtime
1646: * files of a component without requiring undeployment of Service Assemblies
1647: * with Service Units deployed to the component.
1648: * @param componentName The name of the component.
1649: * @param installZipPath The Path to the component archive.
1650: * @throws JBIException if there is a problem with the upgrade.
1651: */
1652:
1653: private void doUpgradeComponent(String componentName,
1654: String installZipPath) throws javax.jbi.JBIException {
1655: String installZipURL;
1656: boolean isUpgradeImplemented;
1657:
1658: //Step 1: Call framework to validate. Upon failure call cancelUpgrade
1659: try {
1660: installZipURL = convertToProperURL(installZipPath)
1661: .getFile();
1662: isUpgradeImplemented = validateComponentForUpgrade(
1663: componentName, installZipURL);
1664:
1665: } catch (JBIException jbiEx) {
1666: mEnv.getComponentManager().cancelComponentUpgrade(
1667: componentName);
1668: mLogger.warning(jbiEx.toString());
1669: throw jbiEx;
1670: } catch (MalformedURLException urlEx) {
1671: mEnv.getComponentManager().cancelComponentUpgrade(
1672: componentName);
1673: mLogger.warning(urlEx.toString());
1674: throw new JBIException(urlEx);
1675: }
1676:
1677: //Step 2: finished validation. call framework to actually upgrade
1678: try {
1679: if (mEnv.getPlatformContext().isAdminServer()) {
1680: upgradeComponentInAdminServer(componentName,
1681: isUpgradeImplemented);
1682: } else {
1683: upgradeComponentInInstance(componentName,
1684: installZipURL, isUpgradeImplemented);
1685: }
1686: mLogger.info(mTranslator.getString(
1687: LocalStringKeys.IS_COMPONENT_UPGRADE_SUCCESSFUL,
1688: componentName));
1689:
1690: } catch (JBIException jbiEx) {
1691: mLogger.warning(mTranslator.getString(
1692: LocalStringKeys.IS_COMPONENT_UPGRADE_FAILED,
1693: componentName));
1694: mLogger.warning(jbiEx.toString());
1695: throw jbiEx;
1696: }
1697: }
1698:
1699: /**
1700: * This method is used to upgrade a component in an admin server instance.
1701: * The component install root is updated in the facade installation service.
1702: * This method invokes the framework upgrade component (after saving SU roots
1703: * if needed).
1704: * @param componentName the component name.
1705: * @param isUpgradeImplemented true if the component involved implements upgrade SPI.
1706: * @throws JBIException if there are problems in updating the component.
1707: */
1708: private void upgradeComponentInAdminServer(String componentName,
1709: boolean isUpgradeImplemented) throws JBIException {
1710:
1711: //in DAS component install root has been updated by facade
1712:
1713: String suBackupDir = null;
1714: //1. backup SUs if needed
1715: try {
1716: if (isUpgradeImplemented) {
1717: suBackupDir = backupSUsDeployedOnComponent(componentName);
1718: }
1719: } catch (ManagementException mEx) {
1720: throw new JBIException(mEx.getMessage());
1721: }
1722:
1723: //2. call framework to upgrade component
1724: try {
1725: invokeFrameworkUpgradeComponent(componentName);
1726: } catch (JBIException jbiEx) {
1727: try {
1728: if (isUpgradeImplemented) {
1729: restoreSUsDeployedOnComponent(componentName,
1730: suBackupDir);
1731: }
1732: } catch (ManagementException mEx) {
1733: mLogger.warning(mEx.getMessage());
1734: }
1735: //we do not restore component install root for 'server' because it
1736: //will affect the domain repository
1737: mLogger
1738: .finer("Component update failed in server, install root not restored");
1739: throw jbiEx;
1740: }
1741: }
1742:
1743: /**
1744: * This method is used to upgrade a component in a non admin server instance
1745: * This method takes a back up of the existing install root and replaces it
1746: * using the given archive. With the updated install root the upgradeComponent
1747: * method in framework is invoked. If there are failures the install root is
1748: * restored. If the upgrade was successful the upgrade-number is incremented
1749: * @param componentName the component name
1750: * @param installZipURL the archive path
1751: * @param isUpgradeImplemented true if the component implements upgrade SPI
1752: * @throws JBIException if there are problems in updating the component
1753: */
1754: private void upgradeComponentInInstance(String componentName,
1755: String installZipURL, boolean isUpgradeImplemented)
1756: throws JBIException {
1757: //1. backup existing install root
1758: mLogger.finer("Entered upgradeComponentInInstance with file "
1759: + installZipURL);
1760: String backupDir;
1761: String suBackupDir = null;
1762: String componentRoot;
1763: String existingArchiveName = null;
1764: Archive upgradeArchive = null;
1765: try {
1766: existingArchiveName = getRegistry().getGenericQuery()
1767: .getComponentFileName(componentName);
1768:
1769: //componentRoot will be <instanceRoot>/jbi/components/sun-http-binding
1770: componentRoot = new File(getInstallRoot(
1771: ArchiveType.COMPONENT, componentName)).getParent();
1772:
1773: backupDir = backupInstallRoot(componentRoot, componentName);
1774: mLogger.finer("Successfully saved install root in "
1775: + backupDir);
1776: } catch (IOException ioEx) {
1777: mEnv.getComponentManager().cancelComponentUpgrade(
1778: componentName);
1779: mLogger.warning(ioEx.toString());
1780: throw new JBIException(
1781: mTranslator
1782: .getString(
1783: LocalStringKeys.IS_COMPONENT_UPGRADE_INSTALL_ROOT_BACKUP_FAILED,
1784: componentName)
1785: + ioEx.getMessage());
1786: } catch (RegistryException regEx) {
1787: mEnv.getComponentManager().cancelComponentUpgrade(
1788: componentName);
1789: mLogger.warning(regEx.toString());
1790: throw new JBIException(
1791: mTranslator
1792: .getString(
1793: LocalStringKeys.IS_COMPONENT_UPGRADE_INSTALL_ROOT_BACKUP_FAILED,
1794: componentName)
1795: + regEx.getMessage());
1796:
1797: }
1798:
1799: //2. remove existing install root
1800: try {
1801: getRegistry().getRepository().removeArchive(
1802: ArchiveType.COMPONENT, componentName);
1803: mLogger
1804: .finer("Successfully removed install root from repository");
1805: } catch (RepositoryException repEx) {
1806: mEnv.getComponentManager().cancelComponentUpgrade(
1807: componentName);
1808: restoreInstallRoot(componentName, backupDir, componentRoot);
1809: mLogger.warning(repEx.toString());
1810: throw new JBIException(
1811: mTranslator
1812: .getString(
1813: LocalStringKeys.IS_COMPONENT_UPGRADE_INSTALL_ROOT_NOT_DELETED,
1814: componentName)
1815: + repEx.getMessage());
1816: }
1817:
1818: //3. verify if install root is really gone
1819: if (new File(componentRoot).exists()) {
1820: mLogger
1821: .finer("Install root was not completely deleted from repository");
1822: mEnv.getComponentManager().cancelComponentUpgrade(
1823: componentName);
1824: restoreInstallRoot(componentName, backupDir, componentRoot);
1825: String message = mTranslator
1826: .getString(
1827: LocalStringKeys.IS_COMPONENT_UPGRADE_INSTALL_ROOT_NOT_DELETED,
1828: componentName);
1829: mLogger.warning(message);
1830: throw new JBIException(message);
1831: }
1832:
1833: //4. create new install root and bring over the old workspace
1834: try {
1835: upgradeArchive = getRegistry().getRepository().addArchive(
1836: ArchiveType.COMPONENT, installZipURL);
1837: mLogger
1838: .finer("Successfully added new archive in repository");
1839: copyWorkspaceFromBackup(backupDir, componentName,
1840: componentRoot);
1841: mLogger.finer("Successfully recreated component workspace");
1842: copyLoggerSettingsFromBackup(backupDir, componentName,
1843: componentRoot);
1844: mLogger.finer("Successfully recreated logger settings");
1845: } catch (IOException ioEx) {
1846: mEnv.getComponentManager().cancelComponentUpgrade(
1847: componentName);
1848: restoreInstallRoot(componentName, backupDir, componentRoot);
1849: mLogger.warning(ioEx.toString());
1850: throw new JBIException(
1851: mTranslator
1852: .getString(LocalStringKeys.IS_COMPONENT_UPGRADE_INSTALL_ROOT_NOT_CREATED)
1853: + ioEx.getMessage());
1854: } catch (RepositoryException repEx) {
1855: mEnv.getComponentManager().cancelComponentUpgrade(
1856: componentName);
1857: restoreInstallRoot(componentName, backupDir, componentRoot);
1858: mLogger.warning(repEx.toString());
1859: throw new JBIException(
1860: mTranslator
1861: .getString(
1862: LocalStringKeys.IS_COMPONENT_UPGRADE_WORKSPACE_NOT_RESTORED,
1863: componentName)
1864: + repEx.getMessage());
1865: }
1866:
1867: //5. backup the SUs if upgrade SPI is implemented by component
1868: try {
1869: if (isUpgradeImplemented) {
1870: suBackupDir = backupSUsDeployedOnComponent(componentName);
1871: }
1872: } catch (ManagementException mEx) {
1873: restoreInstallRoot(componentName, backupDir, componentRoot);
1874: mLogger.warning(mEx.toString());
1875: throw new JBIException(mEx);
1876: }
1877:
1878: //6. now call framework to upgrade the component
1879: try {
1880: invokeFrameworkUpgradeComponent(componentName);
1881: mLogger
1882: .finer("Successfully upgradedthe component in framework");
1883: incrementComponentUpgradeNumber(componentName);
1884: } catch (JBIException jbiEx) {
1885: //cleanup the new contents before restore
1886: restoreInstallRoot(componentName, backupDir, componentRoot);
1887:
1888: try {
1889: if (isUpgradeImplemented) {
1890: restoreSUsDeployedOnComponent(componentName,
1891: suBackupDir);
1892: }
1893: } catch (ManagementException mEx) {
1894: mLogger.warning(mEx.getMessage());
1895: }
1896: mLogger.warning(jbiEx.toString());
1897: throw jbiEx;
1898: }
1899:
1900: //7.if archive names are different modify registry
1901: modifyArchiveNameInRegistry(componentName, existingArchiveName,
1902: upgradeArchive.getFileName());
1903:
1904: //8.the descriptors will be different, remove old one from cache
1905: ((com.sun.jbi.management.registry.xml.GenericQueryImpl) getRegistry()
1906: .getGenericQuery())
1907: .removeComponentFromCache(componentName);
1908:
1909: //we want to preserve the backup copy in instances too.
1910: /*File backup = new File(backupDir);
1911: FileHelper.cleanDirectory(backup);
1912: backup.delete(); */
1913:
1914: }
1915:
1916: /**
1917: * This method is used to increment the upgrade number for a component by one
1918: * @param componentName the component name
1919: * @throws RegistryException if the upgrade number could not be incremented
1920: */
1921: private void incrementComponentUpgradeNumber(String componentName)
1922: throws RegistryException {
1923: BigInteger currNumber = getRegistry().getGenericQuery()
1924: .getComponentUpgradeNumber(componentName);
1925: getRegistry().getUpdater().setComponentUpgradeNumber(
1926: componentName, currNumber.add(BigInteger.ONE));
1927: mLogger.fine("Incremented upgrade number for component"
1928: + componentName);
1929: }
1930:
1931: /**
1932: * This method is used to set the upgrade number for a component
1933: * @param componentName the component name
1934: * @param upgradeNumber the upgrade number to be set
1935: * @throws RegistryException if the upgrade number could not be incremented
1936: */
1937: private void setComponentUpgradeNumber(String componentName,
1938: long upgradeNumber) throws RegistryException {
1939: getRegistry().getUpdater().setComponentUpgradeNumber(
1940: componentName, BigInteger.valueOf(upgradeNumber));
1941: mLogger
1942: .fine("Set upgrade number for component"
1943: + componentName);
1944: }
1945:
1946: /**
1947: * This method is used to backup the SUs deployed to a component.
1948: * @param componentName the component name
1949: * @return String the dir where the SUs are saved
1950: * @throws ManagementException if there are issues in copying the SU roots
1951: */
1952: private String backupSUsDeployedOnComponent(String componentName)
1953: throws ManagementException {
1954: String backupRoot = null;
1955: try {
1956: backupRoot = mEnv.getJbiInstanceRoot()
1957: + File.separator
1958: + TMP_DIR
1959: + File.separator
1960: + componentName
1961: + UNDERSCORE
1962: + getRegistry().getGenericQuery()
1963: .getComponentUpgradeNumber(componentName)
1964: + UNDERSCORE + SU_BACKUP + UNDERSCORE
1965: + FileHelper.getTimestamp();
1966:
1967: ComponentInfo compInfo = getComponentQuery()
1968: .getComponentInfo(componentName);
1969: if (compInfo != null) {
1970: List<ServiceUnitInfo> suList = compInfo
1971: .getServiceUnitList();
1972:
1973: for (ServiceUnitInfo suInfo : suList) {
1974: String backupDir = backupRoot + File.separator
1975: + suInfo.getServiceAssemblyName()
1976: + File.separator + suInfo.getName();
1977:
1978: String saRoot = mEnv.getJbiInstanceRoot()
1979: + File.separator
1980: + REPOSITORY_SERVICE_ASSEMBLY_STORE
1981: + File.separator
1982: + suInfo.getServiceAssemblyName()
1983: + File.separator + suInfo.getName();
1984: mLogger.fine("Saving a copy of SU root from "
1985: + saRoot + " at " + backupDir);
1986: FileHelper.copy(saRoot, backupDir);
1987: }
1988: }
1989: } catch (IOException ex) {
1990: throw new ManagementException(ex);
1991: } catch (RegistryException regEx) {
1992: throw new ManagementException(regEx);
1993: }
1994: return backupRoot;
1995: }
1996:
1997: /**
1998: * This method is used to restore the SU roots from a backup copy.
1999: * This method is called if the component SPI encountered issues in
2000: * upgrade and we want to restore everything to previous state
2001: * @param componentName the component name
2002: * @param backupDir the dir where the backup is stored
2003: * @throws ManagementException if the SU roots cannot be restored
2004: */
2005: private void restoreSUsDeployedOnComponent(String componentName,
2006: String backupDir) throws ManagementException {
2007: try {
2008: File saList[] = new File(backupDir).listFiles();
2009: for (int i = 0; i < saList.length; i++) {
2010: File suList[] = saList[i].listFiles();
2011: for (int j = 0; j < suList.length; j++) {
2012: String suBackupDir = backupDir + File.separator
2013: + saList[i].getName() + File.separator
2014: + suList[j].getName();
2015:
2016: String suRoot = mEnv.getJbiInstanceRoot()
2017: + File.separator
2018: + REPOSITORY_SERVICE_ASSEMBLY_STORE
2019: + File.separator + saList[i].getName()
2020: + File.separator + suList[j].getName();
2021: if (FileHelper.copy(suBackupDir, suRoot)) {
2022: mLogger
2023: .fine("Successfully restored SU root from "
2024: + suBackupDir + " to " + suRoot);
2025: } else {
2026: mLogger.fine("Could not restore SU root in "
2027: + suRoot + " from " + suBackupDir);
2028: }
2029: }
2030: }
2031: } catch (Exception ioEx) {
2032: throw new ManagementException(ioEx.getMessage());
2033: }
2034: }
2035:
2036: /**
2037: * This method is used to modify the name of the component archive in
2038: * registry. This is needed to address the cases where the name of the
2039: * archive given for upgrade is different from the current
2040: * one.
2041: * @param componentName the component name
2042: * @param exisitingName the archive name that is in registry
2043: * @param newName the new archive name given for upgrade
2044: * @param installZipURL the URL for achive provided for upgrade
2045: */
2046: private void modifyArchiveNameInRegistry(String componentName,
2047: String existingName, String newName)
2048: throws RegistryException, ManagementException {
2049: if (!existingName.equals(newName)) {
2050: getRegistry().getUpdater().setComponentFileName(
2051: componentName, newName);
2052: }
2053: }
2054: }
|