001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)EnvironmentSetup.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: import com.sun.jbi.JBIProvider;
032: import com.sun.jbi.framework.ScaffoldPlatformContext;
033:
034: import java.io.File;
035: import java.util.logging.Level;
036: import javax.management.MBeanServerFactory;
037: import javax.naming.InitialContext;
038:
039: /**
040: * Utility class to provide the setup for unit tests that require a running
041: * environment.
042: *
043: * @author Sun Microsystems, Inc.
044: */
045: public class EnvironmentSetup {
046: /**
047: * Value of the $SRCROOT environment variable
048: */
049: private String mSrcroot;
050:
051: /**
052: * Value of the $AS8BASE environment variable
053: */
054: private String mAS8base;
055:
056: /**
057: * JBI install root directory
058: */
059: private String mJbiroot;
060:
061: /**
062: * JBI instance root directory
063: */
064: private String mJbiInstanceRoot;
065:
066: /**
067: * Local instance of the EnvironmentContext class
068: */
069: private EnvironmentContext mContext;
070:
071: /**
072: * The appserver domain root directory.
073: */
074: private static final String DOMAIN_ROOT = "domains/domain1";
075:
076: /**
077: * The JBI installation root directory.
078: */
079: private static final String JBI_INSTALL_ROOT = "jbi";
080:
081: /**
082: * One second (in milliseconds) for timeout properties.
083: */
084: private static final String TIMEOUT_STRING = "1000";
085:
086: /**
087: * One second (in milliseconds) for timeout values.
088: */
089: private static final int TIMEOUT = 1000;
090:
091: /**
092: * Constructor.
093: * @throws Exception when set up fails for any reason.
094: */
095: public EnvironmentSetup() throws Exception {
096: mSrcroot = System.getProperty("junit.srcroot");
097: mSrcroot = mSrcroot.replace('\\', '/');
098: mAS8base = System.getProperty("junit.as8base") + "/";
099: mJbiroot = mAS8base + JBI_INSTALL_ROOT;
100: mJbiInstanceRoot = mAS8base + DOMAIN_ROOT + "/"
101: + JBI_INSTALL_ROOT;
102:
103: System.err.println("srcroot = " + mSrcroot);
104: System.err.println("AS8Base = " + mAS8base);
105: System.err.println("JBI install root = " + mJbiroot);
106: System.err.println("JBI instance root = " + mJbiInstanceRoot);
107:
108: // Create and initialize the EnvironmentContext.
109:
110: java.util.Properties props = new java.util.Properties();
111: props.setProperty("com.sun.jbi.defaultLogLevel", Level.FINEST
112: .toString());
113:
114: // Create a new framework instance with state = ready
115: JBIFramework framework = new JBIFramework();
116: framework.setFrameworkReady();
117:
118: ScaffoldPlatformContext platform = new ScaffoldPlatformContext();
119: platform.setInstallRoot(mAS8base);
120: platform.setInstanceRoot(mAS8base + DOMAIN_ROOT);
121: mContext = new ScaffoldEnvironmentContext(platform, framework,
122: props);
123: mContext.setComponentTimeout(TIMEOUT);
124: mContext.setDeploymentTimeout(TIMEOUT);
125: mContext.setInstallationTimeout(TIMEOUT);
126: mContext.setServiceUnitTimeout(TIMEOUT);
127: mContext.setJbiInstallRoot(mJbiroot);
128: mContext.setJbiInstanceRoot(mJbiInstanceRoot);
129: mContext.setNamingContext(new InitialContext());
130: mContext.setRegistry(new ScaffoldRegistry(mContext));
131: }
132:
133: /**
134: * Get the EnvironmentContext.
135: * @return The EnvironmentContext created by the constructor.
136: */
137: public EnvironmentContext getEnvironmentContext() {
138: return mContext;
139: }
140:
141: /**
142: * Startup the test environment.
143: * @param startComponentRegistry - set to true to start the Component
144: * Registry.
145: * @param startComponentFramework - set to true to start the Component
146: * Framework.
147: * @throws Exception when startup fails for any reason.
148: */
149: public void startup(boolean startComponentRegistry,
150: boolean startComponentFramework) throws Exception {
151: if (startComponentRegistry) {
152: com.sun.jbi.ServiceLifecycle cr = (com.sun.jbi.ServiceLifecycle) mContext
153: .getComponentRegistry();
154: cr.initService(mContext);
155: cr.startService();
156: }
157: if (startComponentFramework) {
158: com.sun.jbi.ServiceLifecycle cf = (com.sun.jbi.ServiceLifecycle) mContext
159: .getComponentFramework();
160: cf.initService(mContext);
161: cf.startService();
162: }
163: }
164:
165: /**
166: * Shutdown the test environment.
167: * @param stopComponentRegistry - set to true to stop the Component
168: * Registry.
169: * @param stopComponentFramework - set to true to stop the Component
170: * Framework.
171: * @throws Exception when shutdown fails for any reason.
172: */
173: public void shutdown(boolean stopComponentRegistry,
174: boolean stopComponentFramework) throws Exception {
175: if (stopComponentFramework) {
176: mContext.getComponentFramework().stopService();
177: }
178: if (stopComponentRegistry) {
179: mContext.getComponentRegistry().stopService();
180: }
181: }
182: }
|