001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.jbi.container;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.util.Map;
022: import java.util.concurrent.ConcurrentHashMap;
023: import java.util.concurrent.atomic.AtomicBoolean;
024:
025: import javax.jbi.JBIException;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
030: import org.apache.servicemix.jbi.management.BaseSystemService;
031: import org.apache.servicemix.jbi.util.FileUtil;
032: import org.apache.servicemix.jbi.util.FileVersionUtil;
033:
034: /**
035: * Holder for environment information
036: *
037: * <component-name> (component root dir)
038: * |-> version_X (versionned dir)
039: * \-> workspace (workspace dir)
040: *
041: * ServiceAssembly root
042: * \-> version_X (versionned dir)
043: * |-> install (unzip dir)
044: * \-> sus (service units dir)
045: * |-> <component-name>
046: * |-> <service-unit-name>
047: *
048: * @version $Revision: 564900 $
049: */
050: public class EnvironmentContext extends BaseSystemService implements
051: EnvironmentContextMBean {
052:
053: private static final Log LOG = LogFactory
054: .getLog(EnvironmentContext.class);
055:
056: private File jbiRootDir;
057: private File componentsDir;
058: private File installationDir;
059: private File deploymentDir;
060: private File sharedLibDir;
061: private File serviceAssembliesDir;
062: private File tmpDir;
063: private Map envMap = new ConcurrentHashMap();
064: private AtomicBoolean started = new AtomicBoolean(false);
065:
066: /**
067: * @return the current version of servicemix
068: */
069: public static String getVersion() {
070: String answer = null;
071: Package p = Package.getPackage("org.apache.servicemix");
072: if (p != null) {
073: answer = p.getImplementationVersion();
074: }
075: return answer;
076: }
077:
078: /**
079: * Get Description
080: * @return description
081: */
082: public String getDescription() {
083: return "Manages Environment for the Container";
084: }
085:
086: /**
087: * @return Returns the componentsDir.
088: */
089: public File getComponentsDir() {
090: return componentsDir;
091: }
092:
093: /**
094: * @return Returns the installationDir.
095: */
096: public File getInstallationDir() {
097: return installationDir;
098: }
099:
100: /**
101: * Set the installationDir - rge default location
102: * is root/<container name>/installation
103: * @param installationDir
104: */
105: public void setInstallationDir(File installationDir) {
106: this .installationDir = installationDir;
107: }
108:
109: /**
110: * @return Returns the deploymentDir.
111: */
112: public File getDeploymentDir() {
113: return deploymentDir;
114: }
115:
116: /**
117: * @param deploymentDir The deploymentDir to set.
118: */
119: public void setDeploymentDir(File deploymentDir) {
120: this .deploymentDir = deploymentDir;
121: }
122:
123: /**
124: *
125: * @return Returns the shared library directory
126: */
127: public File getSharedLibDir() {
128: return sharedLibDir;
129: }
130:
131: /**
132: * @return Returns the tmpDir
133: */
134: public File getTmpDir() {
135: if (tmpDir != null) {
136: FileUtil.buildDirectory(tmpDir);
137: }
138: return tmpDir;
139: }
140:
141: /**
142: * @return Returns service asseblies directory
143: */
144: public File getServiceAssembliesDir() {
145: return serviceAssembliesDir;
146: }
147:
148: /**
149: * Initialize the Environment
150: *
151: * @param container
152: * @param rootDirPath
153: * @exception javax.jbi.JBIException if the root directory informed could not be created or it is not a directory
154: */
155: public void init(JBIContainer container, String rootDirPath)
156: throws JBIException {
157: super .init(container);
158: jbiRootDir = new File(rootDirPath);
159: buildDirectoryStructure();
160: }
161:
162: protected Class getServiceMBean() {
163: return EnvironmentContextMBean.class;
164: }
165:
166: /**
167: * Start the item.
168: *
169: * @exception javax.jbi.JBIException if the item fails to start.
170: */
171: public void start() throws javax.jbi.JBIException {
172: if (started.compareAndSet(false, true)) {
173: super .start();
174: }
175: }
176:
177: /**
178: * Stop the item. This suspends current messaging activities.
179: *
180: * @exception javax.jbi.JBIException if the item fails to stop.
181: */
182: public void stop() throws javax.jbi.JBIException {
183: if (started.compareAndSet(true, false)) {
184: super .stop();
185: }
186: }
187:
188: /**
189: * Shut down the item. The releases resources, preparatory to uninstallation.
190: *
191: * @exception javax.jbi.JBIException if the item fails to shut down.
192: */
193: public void shutDown() throws javax.jbi.JBIException {
194: super .shutDown();
195: envMap.clear();
196: container.getManagementContext().unregisterMBean(this );
197: }
198:
199: /**
200: * register the ComponentConnector
201: *
202: * @param connector
203: * @return the CompponentEnvironment
204: * @throws JBIException
205: */
206: public ComponentEnvironment registerComponent(
207: ComponentMBeanImpl connector) throws JBIException {
208: return registerComponent(null, connector);
209: }
210:
211: /**
212: * register the ComponentConnector
213: *
214: * @param connector
215: * @return the CompponentEnvironment
216: * @throws JBIException
217: */
218: public ComponentEnvironment registerComponent(
219: ComponentEnvironment result, ComponentMBeanImpl connector)
220: throws JBIException {
221: if (result == null) {
222: result = new ComponentEnvironment();
223: }
224: if (!connector.isPojo()) {
225: if (container.isEmbedded()) {
226: throw new JBIException(
227: "JBI component can not be installed in embedded mode");
228: }
229: // add workspace root and stats root ..
230: try {
231: String name = connector.getComponentNameSpace()
232: .getName();
233: if (result.getComponentRoot() == null) {
234: File componentRoot = getComponentRootDir(name);
235: FileUtil.buildDirectory(componentRoot);
236: result.setComponentRoot(componentRoot);
237: }
238: if (result.getWorkspaceRoot() == null) {
239: File privateWorkspace = createWorkspaceDirectory(name);
240: result.setWorkspaceRoot(privateWorkspace);
241: }
242: if (result.getStateFile() == null) {
243: File stateFile = FileUtil.getDirectoryPath(result
244: .getComponentRoot(), "state.xml");
245: result.setStateFile(stateFile);
246: }
247: } catch (IOException e) {
248: throw new JBIException(e);
249: }
250: }
251: result.setLocalConnector(connector);
252: envMap.put(connector, result);
253: return result;
254: }
255:
256: /**
257: * Get root directory for a Component
258: *
259: * @param componentName
260: * @return directory for deployment/workspace etc
261: * @throws IOException
262: */
263: public File getComponentRootDir(String componentName) {
264: if (getComponentsDir() == null) {
265: return null;
266: }
267: return FileUtil.getDirectoryPath(getComponentsDir(),
268: componentName);
269: }
270:
271: /**
272: * Create root directory for a Component
273: *
274: * @param componentName
275: * @return directory for deployment/workspace etc
276: * @throws IOException
277: */
278: public File createComponentRootDir(String componentName)
279: throws IOException {
280: if (getComponentsDir() == null) {
281: return null;
282: }
283: return FileUtil.getDirectoryPath(getComponentsDir(),
284: componentName);
285: }
286:
287: /**
288: * Get a new versionned directory for installation
289: *
290: * @param componentName
291: * @return
292: * @throws IOException
293: */
294: public File getNewComponentInstallationDir(String componentName)
295: throws IOException {
296: File result = getComponentRootDir(componentName);
297: // get new version dir
298: return FileVersionUtil.getNewVersionDirectory(result);
299: }
300:
301: /**
302: * Create installation directory for a Component
303: *
304: * @param componentName
305: * @return directory to deploy in
306: * @throws IOException
307: */
308: public File getComponentInstallationDir(String componentName)
309: throws IOException {
310: File result = getComponentRootDir(componentName);
311: // get the version directory
312: return FileVersionUtil.getLatestVersionDirectory(result);
313: }
314:
315: public ComponentEnvironment getNewComponentEnvironment(
316: String compName) throws IOException {
317: File rootDir = FileUtil.getDirectoryPath(getComponentsDir(),
318: compName);
319: File instDir = FileVersionUtil.getNewVersionDirectory(rootDir);
320: File workDir = FileUtil.getDirectoryPath(rootDir, "workspace");
321: File stateFile = FileUtil
322: .getDirectoryPath(rootDir, "state.xml");
323: ComponentEnvironment env = new ComponentEnvironment();
324: env.setComponentRoot(rootDir);
325: env.setInstallRoot(instDir);
326: env.setWorkspaceRoot(workDir);
327: env.setStateFile(stateFile);
328: return env;
329: }
330:
331: public ComponentEnvironment getComponentEnvironment(String compName)
332: throws IOException {
333: File rootDir = FileUtil.getDirectoryPath(getComponentsDir(),
334: compName);
335: File instDir = FileVersionUtil
336: .getLatestVersionDirectory(rootDir);
337: File workDir = FileUtil.getDirectoryPath(rootDir, "workspace");
338: File stateFile = FileUtil
339: .getDirectoryPath(rootDir, "state.xml");
340: ComponentEnvironment env = new ComponentEnvironment();
341: env.setComponentRoot(rootDir);
342: env.setInstallRoot(instDir);
343: env.setWorkspaceRoot(workDir);
344: env.setStateFile(stateFile);
345: return env;
346: }
347:
348: public ServiceAssemblyEnvironment getNewServiceAssemblyEnvironment(
349: String saName) throws IOException {
350: File rootDir = FileUtil.getDirectoryPath(
351: getServiceAssembliesDir(), saName);
352: File versDir = FileVersionUtil.getNewVersionDirectory(rootDir);
353: File instDir = FileUtil.getDirectoryPath(versDir, "install");
354: File susDir = FileUtil.getDirectoryPath(versDir, "sus");
355: File stateFile = FileUtil
356: .getDirectoryPath(rootDir, "state.xml");
357: ServiceAssemblyEnvironment env = new ServiceAssemblyEnvironment();
358: env.setRootDir(rootDir);
359: env.setInstallDir(instDir);
360: env.setSusDir(susDir);
361: env.setStateFile(stateFile);
362: return env;
363: }
364:
365: public ServiceAssemblyEnvironment getServiceAssemblyEnvironment(
366: String saName) {
367: File rootDir = FileUtil.getDirectoryPath(
368: getServiceAssembliesDir(), saName);
369: File versDir = FileVersionUtil
370: .getLatestVersionDirectory(rootDir);
371: File instDir = FileUtil.getDirectoryPath(versDir, "install");
372: File susDir = FileUtil.getDirectoryPath(versDir, "sus");
373: File stateFile = FileUtil
374: .getDirectoryPath(rootDir, "state.xml");
375: ServiceAssemblyEnvironment env = new ServiceAssemblyEnvironment();
376: env.setRootDir(rootDir);
377: env.setInstallDir(instDir);
378: env.setSusDir(susDir);
379: env.setStateFile(stateFile);
380: return env;
381: }
382:
383: /**
384: * Create workspace directory for a Component
385: *
386: * @param componentName
387: * @return directory workspace
388: * @throws IOException
389: */
390: public File createWorkspaceDirectory(String componentName)
391: throws IOException {
392: File result = FileUtil.getDirectoryPath(getComponentsDir(),
393: componentName);
394: result = FileUtil.getDirectoryPath(result, "workspace");
395: FileUtil.buildDirectory(result);
396: return result;
397: }
398:
399: /**
400: * deregister the ComponentConnector
401: *
402: * @param connector
403: * @param doDelete true if component is to be deleted
404: */
405: public void unreregister(ComponentMBeanImpl connector) {
406: this .envMap.remove(connector);
407: }
408:
409: /**
410: * Remove the Component root directory from the local file system
411: *
412: * @param componentName
413: */
414: public void removeComponentRootDirectory(String componentName) {
415: File file = getComponentRootDir(componentName);
416: if (file != null) {
417: if (!FileUtil.deleteFile(file)) {
418: LOG
419: .warn("Failed to remove directory structure for component [version]: "
420: + componentName
421: + " ["
422: + file.getName()
423: + ']');
424: } else {
425: LOG
426: .info("Removed directory structure for component [version]: "
427: + componentName
428: + " ["
429: + file.getName()
430: + ']');
431: }
432: }
433: }
434:
435: /**
436: * create a shared library directory
437: *
438: * @param name
439: * @return directory
440: * @throws IOException
441: */
442: public File createSharedLibraryDirectory(String name) {
443: File result = FileUtil
444: .getDirectoryPath(getSharedLibDir(), name);
445: FileUtil.buildDirectory(result);
446: return result;
447: }
448:
449: /**
450: * remove shared library directory
451: * @param name
452: * @throws IOException
453: */
454: public void removeSharedLibraryDirectory(String name) {
455: File result = FileUtil
456: .getDirectoryPath(getSharedLibDir(), name);
457: FileUtil.deleteFile(result);
458: }
459:
460: private void buildDirectoryStructure() throws JBIException {
461: // We want ServiceMix to be able to run embedded
462: // so do not create the directory structure if the root does not exist
463: if (container.isEmbedded()) {
464: return;
465: }
466: try {
467: jbiRootDir = jbiRootDir.getCanonicalFile();
468: if (!jbiRootDir.exists()) {
469: if (!jbiRootDir.mkdirs()) {
470: throw new JBIException(
471: "Directory could not be created: "
472: + jbiRootDir.getCanonicalFile());
473: }
474: } else if (!jbiRootDir.isDirectory()) {
475: throw new JBIException("Not a directory: "
476: + jbiRootDir.getCanonicalFile());
477: }
478: if (installationDir == null) {
479: installationDir = FileUtil.getDirectoryPath(jbiRootDir,
480: "install");
481: }
482: installationDir = installationDir.getCanonicalFile();
483: if (deploymentDir == null) {
484: deploymentDir = FileUtil.getDirectoryPath(jbiRootDir,
485: "deploy");
486: }
487: deploymentDir = deploymentDir.getCanonicalFile();
488: componentsDir = FileUtil.getDirectoryPath(jbiRootDir,
489: "components").getCanonicalFile();
490: tmpDir = FileUtil.getDirectoryPath(jbiRootDir, "tmp")
491: .getCanonicalFile();
492: sharedLibDir = FileUtil.getDirectoryPath(jbiRootDir,
493: "sharedlibs").getCanonicalFile();
494: serviceAssembliesDir = FileUtil.getDirectoryPath(
495: jbiRootDir, "service-assemblies")
496: .getCanonicalFile();
497: //actually create the sub directories
498: FileUtil.buildDirectory(installationDir);
499: FileUtil.buildDirectory(deploymentDir);
500: FileUtil.buildDirectory(componentsDir);
501: FileUtil.buildDirectory(tmpDir);
502: FileUtil.buildDirectory(sharedLibDir);
503: FileUtil.buildDirectory(serviceAssembliesDir);
504: } catch (IOException e) {
505: throw new JBIException(e);
506: }
507: }
508:
509: public File getJbiRootDir() {
510: return jbiRootDir;
511: }
512:
513: }
|