KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > deployment > DefaultDeploymentServiceImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.deployment;
19
20 import org.sape.carbon.core.component.ComponentConfiguration;
21 import org.sape.carbon.core.component.lifecycle.Configurable;
22 import org.sape.carbon.core.component.lifecycle.Startable;
23 import org.sape.carbon.core.component.lifecycle.Suspendable;
24 import org.sape.carbon.core.config.Config;
25 import org.sape.carbon.core.config.InvalidConfigurationException;
26 import org.sape.carbon.core.config.node.Folder;
27 import org.sape.carbon.core.config.node.Node;
28 import org.sape.carbon.core.config.node.NodeCreationException;
29 import org.sape.carbon.core.config.node.NodeNotFoundException;
30 import org.sape.carbon.core.config.node.NodeRemovalException;
31 import org.sape.carbon.core.config.node.link.GenericLinkNodeConfiguration;
32 import org.sape.carbon.core.config.node.link.GenericLinkNodeFactory;
33
34 import org.sape.carbon.services.deployment.namelookup.*;
35
36 /**
37  * This implementation of <code>DeploymentService</code> uses
38  * BootStrapper.getEnvironmentName and BootStrapper.getInstanceName to
39  * determine which environment and instance the current deployment is.
40  *
41  * @since carbon 1.0
42  * @author Douglas Voet, April 2002
43  * @version $Revision: 1.16 $($Author: dvoet $ / $Date: 2003/09/24 20:31:11 $)
44  * <br>Copyright 2002 Sapient
45  */

46 public class DefaultDeploymentServiceImpl
47     implements DeploymentService, Configurable, Startable, Suspendable {
48
49     /** Holds a reference to the Node that contains deployment information. */
50     protected Folder deploymentsNode;
51
52     /** Holds name of the current deployment node. */
53     protected String JavaDoc currentDeploymentNodeName;
54
55     /** Holds the name of the node with current deployment info. */
56     protected String JavaDoc targetNodeName;
57
58     /**
59      * Fetches the deployment node and stores the CurrentDeploymentNodeName
60      * @see Configurable#configure(ComponentConfiguration)
61      */

62     public void configure(ComponentConfiguration configuration)
63         throws DeploymentServiceException {
64             
65         DeploymentServiceConfiguration deploymentConfiguration =
66             (DeploymentServiceConfiguration) configuration;
67
68         try {
69             this.deploymentsNode =
70                 (Folder) Config.getInstance().fetchNode(
71                     deploymentConfiguration.getDeploymentsNodeAbsoluteName());
72         } catch (NodeNotFoundException nnfe) {
73             throw new InvalidConfigurationException(
74                 this.getClass(),
75                 configuration.getConfigurationName(),
76                 "DeploymentsNodeAbsoluteName",
77                 nnfe);
78         }
79
80         this.currentDeploymentNodeName =
81             deploymentConfiguration.getCurrentDeploymentNodeName();
82             
83         this.targetNodeName = constructTargetNodeName(configuration);
84     }
85
86     /**
87      * Creates the link to the current deployment.
88      * @see Startable#start()
89      */

90     public void start() throws DeploymentServiceException {
91         createDeploymentLink();
92     }
93
94     /**
95      * @see Startable#stop()
96      */

97     public void stop() {
98     }
99
100     /**
101      * Assumes the service has been reconfigured and recreates the link to
102      * the current deployment.
103      * @see Suspendable#resume()
104      */

105     public void resume() throws DeploymentServiceException {
106         // assume the service was reconfigured recreate the
107
// current deployment link
108
createDeploymentLink();
109     }
110
111     /**
112      * @see Suspendable#suspend()
113      */

114     public void suspend() {
115     }
116
117     /**
118      * Helper method that does the work of creating the link to the
119      * current deployment configuration.
120      *
121      * @throws DeploymentServiceException indicates an error creating
122      * the deployment link.
123      */

124     protected void createDeploymentLink()
125         throws DeploymentServiceException {
126             
127         try {
128             GenericLinkNodeConfiguration linkConfiguration =
129                 (GenericLinkNodeConfiguration) Config
130                     .getInstance()
131                     .createConfiguration(GenericLinkNodeConfiguration.class);
132
133             linkConfiguration.setLinkNodeFactoryClass(
134                 GenericLinkNodeFactory.class);
135
136             linkConfiguration.setTargetNodeName(this.targetNodeName);
137
138             if (this
139                 .deploymentsNode
140                 .containsChild(this.currentDeploymentNodeName)) {
141
142                 this
143                     .deploymentsNode
144                     .fetchChild(this.currentDeploymentNodeName)
145                     .remove();
146             }
147
148             this.deploymentsNode.addLink(
149                 this.currentDeploymentNodeName,
150                 linkConfiguration);
151
152         } catch (NodeRemovalException nre) {
153             throw new DeploymentServiceException(
154                 this.getClass(),
155                 "Could not remove existing current deployment "
156                     + "link: ["
157                     + this.deploymentsNode.getAbsoluteName()
158                     + Node.DELIMITER
159                     + this.currentDeploymentNodeName
160                     + "]",
161                 nre);
162
163         } catch (NodeNotFoundException nnfe) {
164             // this should never happen
165
// cause is probably a bug in Folder implementation
166
throw new DeploymentServiceException(
167                 this.getClass(),
168                 "Inconsistent results from Folder: containsChild "
169                     + "returned true, but fetch threw NodeNotFoundException",
170                 nnfe);
171
172         } catch (NodeCreationException nce) {
173             throw new DeploymentServiceException(
174                 this.getClass(),
175                 "Could not create current deployment link: ["
176                     + this.deploymentsNode.getAbsoluteName()
177                     + Node.DELIMITER
178                     + this.currentDeploymentNodeName
179                     + "]",
180                 nce);
181         }
182     }
183     
184     /**
185      * Helper method that constructs the name of the node that will be used
186      * as the target of the deployment link. This node holds the actual
187      * configuration values that will be used in this deployment. This method
188      * is called from the configure lifecycle method. Override this method
189      * to change how the target node name is constructed.
190      *
191      * @param serviceConfig the configuration for this service (the config
192      * passed into the configure method)
193      * @return the path to the node that should be the target of the deployment
194      * link. Although it is not required, this should be a relative path.
195      */

196     protected String JavaDoc constructTargetNodeName(
197         ComponentConfiguration serviceConfig) throws DeploymentServiceException {
198             
199         DeploymentServiceConfiguration deploymentConfiguration =
200             (DeploymentServiceConfiguration) serviceConfig;
201
202         String JavaDoc environmentName = deploymentConfiguration.getEnvironmentName();
203         NameLookup environmentNameLookup =
204             deploymentConfiguration.getEnvironmentNameLookup();
205             
206         String JavaDoc instanceName = deploymentConfiguration.getInstanceName();
207         NameLookup instanceNameLookup =
208             deploymentConfiguration.getInstanceNameLookup();
209             
210         // validate configuration
211
// both environmentName and environmentNameLookup cannot be null
212
if ((environmentName == null || "".equals(environmentName)) &&
213             environmentNameLookup == null) {
214             throw new InvalidConfigurationException(
215                 this.getClass(),
216                 serviceConfig.getConfigurationName(),
217                 "EnvironmentName or EnvironmentNameLookup",
218                 "Neither EnvironmentName nor EnvironmentNameLookup were specified");
219         }
220         
221         // only one of environmentName or environmentNameLookup can be specified
222
if ((environmentName != null && !"".equals(environmentName)) &&
223             environmentNameLookup != null) {
224             throw new InvalidConfigurationException(
225                 this.getClass(),
226                 serviceConfig.getConfigurationName(),
227                 "EnvironmentName or EnvironmentNameLookup",
228                 "Only one of EnvironmentName and EnvironmentNameLookup can be specified");
229         }
230
231         // only one of instanceName or instanceNameLookup can be specified
232
if ((instanceName != null && !"".equals(instanceName)) &&
233             instanceNameLookup != null) {
234             throw new InvalidConfigurationException(
235                 this.getClass(),
236                 serviceConfig.getConfigurationName(),
237                 "InstanceName or InstanceNameLookup",
238                 "Only one of InstanceName and InstanceNameLookup can be specified");
239         }
240
241         // contruct name
242
StringBuffer JavaDoc nodeName = new StringBuffer JavaDoc();
243         if (environmentNameLookup != null) {
244             nodeName.append(environmentNameLookup.lookupName());
245         } else {
246             nodeName.append(environmentName);
247         }
248         
249         if (instanceNameLookup != null) {
250             nodeName.append(Node.DELIMITER);
251             nodeName.append(instanceNameLookup.lookupName());
252         } else if (instanceName != null && !"".equals(instanceName)) {
253             nodeName.append(Node.DELIMITER);
254             nodeName.append(instanceName);
255         }
256         
257         return nodeName.toString();
258     }
259 }
Popular Tags