View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.web.deployer.support;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.osgi.framework.Bundle;
22  import org.osgi.framework.BundleContext;
23  import org.springframework.beans.factory.InitializingBean;
24  import org.springframework.osgi.context.BundleContextAware;
25  import org.springframework.osgi.util.OsgiStringUtils;
26  import org.springframework.osgi.web.deployer.OsgiWarDeploymentException;
27  import org.springframework.osgi.web.deployer.WarDeployer;
28  import org.springframework.osgi.web.deployer.WarDeployment;
29  import org.springframework.util.Assert;
30  
31  /**
32   * Convenient base class offering common functionality for war deployers such as
33   * logging.
34   * 
35   * @author Costin Leau
36   */
37  public abstract class AbstractWarDeployer implements WarDeployer, InitializingBean, BundleContextAware {
38  
39  	/** logger */
40  	protected final Log log = LogFactory.getLog(getClass());
41  
42  	private BundleContext bundleContext;
43  
44  
45  	public void afterPropertiesSet() throws Exception {
46  		Assert.notNull(bundleContext, "bundleContext is not set");
47  	}
48  
49  	public void setBundleContext(BundleContext bundleContext) {
50  		this.bundleContext = bundleContext;
51  	}
52  
53  	/**
54  	 * Returns the bundle context used by this deployer.
55  	 * 
56  	 * @return the OSGi bundle context used by this deployer.
57  	 */
58  	protected BundleContext getBundleContext() {
59  		return bundleContext;
60  	}
61  
62  	/**
63  	 * {@inheritDoc}
64  	 * 
65  	 * Breaks down (and logs appropriately) the deployment process into:
66  	 * 
67  	 * <ol>
68  	 * <li>creation of the deployment</li>
69  	 * <li>start-up of the deployment</li>
70  	 * </ol>
71  	 * 
72  	 * Any exception thrown during each step, is wrapped into
73  	 * OsgiWarDeploymentException.
74  	 */
75  	public WarDeployment deploy(Bundle bundle, String contextPath) throws OsgiWarDeploymentException {
76  		String commonMessage = "bundle [" + OsgiStringUtils.nullSafeNameAndSymName(bundle) + "] at [" + contextPath
77  				+ "] on server " + getServerInfo();
78  		if (log.isDebugEnabled())
79  			log.debug("Creating deployment for " + commonMessage);
80  		WarDeployment deployment;
81  
82  		try {
83  			deployment = createDeployment(bundle, contextPath);
84  		}
85  		catch (Exception ex) {
86  			throw new OsgiWarDeploymentException("Cannot create war deployment for " + commonMessage, ex);
87  		}
88  
89  		if (log.isDebugEnabled())
90  			log.debug("About to deploy " + commonMessage);
91  
92  		try {
93  			startDeployment(deployment);
94  			log.info("Successfully deployed " + commonMessage);
95  		}
96  		catch (Exception ex) {
97  			throw new OsgiWarDeploymentException("Cannot create war deployment for " + commonMessage, ex);
98  		}
99  
100 		return deployment;
101 	}
102 
103 	/**
104 	 * Creates and configures (but does not start) the web deployment for the
105 	 * given bundle. The returned object will be passed to
106 	 * {@link #startDeployment(WarDeployment)}.
107 	 * 
108 	 * @param bundle OSGi bundle deployed as war
109 	 * @param contextPath WAR context path
110 	 * @return web deployment artifact
111 	 * @throws Exception if something goes wrong.
112 	 */
113 	protected abstract WarDeployment createDeployment(Bundle bundle, String contextPath) throws Exception;
114 
115 	/**
116 	 * Starts the deployment artifact using the object returned by
117 	 * {@link #createDeployment(Bundle, String)}.
118 	 * 
119 	 * @param deployment web deployment artifact
120 	 * @throws Exception if something goes wrong
121 	 */
122 	protected abstract void startDeployment(WarDeployment deployment) throws Exception;
123 
124 	/**
125 	 * Returns a nice String representation of the underlying server for logging
126 	 * messages.
127 	 * 
128 	 * @return toString for the running environment
129 	 */
130 	protected abstract String getServerInfo();
131 }