getResourcePath(String... resourcePaths) {
81 | URL resourceURL = BaseTest.class.getClassLoader().getResource("");
82 | if (resourceURL != null) {
83 | String resourcePath = resourceURL.getPath();
84 | if (resourcePath != null) {
85 | resourcePath = System.getProperty(OS_NAME_KEY).contains(WINDOWS_PARAM) ?
86 | resourcePath.substring(1) : resourcePath;
87 | return Optional.ofNullable(Paths.get(resourcePath, resourcePaths));
88 | }
89 | }
90 | return Optional.empty(); // Resource do not exist
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/main/java/org/wso2/carbon/deployment/engine/Deployer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine;
17 |
18 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
19 |
20 | import java.net.URL;
21 |
22 | /**
23 | * This interface is used to provide the custom deployment mechanism in carbon, where you
24 | * can write your own Deployer to process a particular {@link ArtifactType}
25 | *
26 | * A developer who wants write a deployer to process an artifact in carbon and add it to a
27 | * runtime configuration, should implement this. The implementation should then be registered
28 | * as an OSGi service using the Deployer interface for the DeploymentEngine to find and add
29 | * to the configuration
30 | *
31 | * @since 5.0.0
32 | */
33 |
34 | public interface Deployer {
35 |
36 | /**
37 | * Initialize the Deployer.
38 | *
39 | * This will contain all the code that need to be called when the deployer is initialized
40 | */
41 | void init();
42 |
43 | /**
44 | * Process a deployable artifact and add it to the relevant runtime configuration.
45 | *
46 | * @param artifact the Artifact object to deploy
47 | * @return returns a key to uniquely identify an artifact within a runtime
48 | * @throws CarbonDeploymentException - when an error occurs while doing the deployment
49 | */
50 | Object deploy(Artifact artifact) throws CarbonDeploymentException;
51 |
52 | /**
53 | * Remove a given artifact from the relevant runtime configuration.
54 | *
55 | * @param key the key of the deployed artifact used for undeploying it from the relevant runtime
56 | * @throws CarbonDeploymentException - when an error occurs while running the undeployment
57 | */
58 | void undeploy(Object key) throws CarbonDeploymentException;
59 |
60 |
61 | /**
62 | * Updates a already deployed artifact and update its relevant runtime configuration.
63 | *
64 | * @param artifact the Artifact object to deploy
65 | * @return returns a key to uniquely identify an artifact within a runtime
66 | * @throws CarbonDeploymentException - when an error occurs while doing the deployment
67 | */
68 | Object update(Artifact artifact) throws CarbonDeploymentException;
69 |
70 | /**
71 | * Returns the deploy directory location associated with the deployer.
72 | *
73 | * It can be relative to CARBON_HOME or an abosolute path
74 | * Eg : webapps, dataservices, sequences or
75 | * /dev/wso2/deployment/repository/ or
76 | * file:/dev/wso2/deployment/repository/
77 | *
78 | * @return deployer directory location
79 | */
80 | URL getLocation();
81 |
82 | /**
83 | * Returns the type of the artifact that the deployer is capable of deploying.
84 | * Eg : webapp, dataservice
85 | *
86 | * @return ArtifactType object which contains info about the artifact type
87 | * @see ArtifactType
88 | */
89 | ArtifactType getArtifactType();
90 |
91 | }
92 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/test/java/org/wso2/carbon/deployment/engine/DeployerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine;
17 |
18 | import org.testng.Assert;
19 | import org.testng.annotations.BeforeTest;
20 | import org.testng.annotations.Test;
21 | import org.wso2.carbon.deployment.engine.deployers.CustomDeployer;
22 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
23 |
24 | import java.io.File;
25 | import java.util.HashMap;
26 |
27 | /**
28 | * Deployer Test class.
29 | *
30 | * @since 5.0.0
31 | */
32 | public class DeployerTest extends BaseTest {
33 | private static final String DEPLOYER_REPO = "carbon-repo" + File.separator + "text-files";
34 | private static final String RUNTIME_DEPLOYER_REPO = "deployment" + File.separator + "text-files";
35 | private CustomDeployer customDeployer;
36 | private Artifact artifact;
37 | private Artifact artifact2;
38 | private String key;
39 | private String key2;
40 |
41 | /**
42 | * @param testName
43 | */
44 | public DeployerTest(String testName) {
45 | super(testName);
46 | }
47 |
48 | @BeforeTest
49 | public void setup() throws CarbonDeploymentException {
50 | customDeployer = new CustomDeployer();
51 | customDeployer.init();
52 | artifact = new Artifact(new File(getTestResourceFile(DEPLOYER_REPO).getAbsolutePath()
53 | + File.separator + "sample1.txt"));
54 | artifact.setVersion("1.0.0");
55 | artifact.setProperties(new HashMap());
56 | artifact2 = new Artifact(new File(getTestResourceFile(RUNTIME_DEPLOYER_REPO).getAbsolutePath()
57 | + File.separator + "sample2.txt"));
58 | artifact2.setVersion("1.0.0");
59 | artifact2.setProperties(new HashMap());
60 | }
61 |
62 | @Test
63 | public void testDeployerInitialization() {
64 | Assert.assertTrue(CustomDeployer.initCalled);
65 | }
66 |
67 | @Test(dependsOnMethods = {"testDeployerInitialization"})
68 | public void testDeploy() throws CarbonDeploymentException {
69 | key = customDeployer.deploy(artifact);
70 | Assert.assertTrue(CustomDeployer.sample1Deployed);
71 | key2 = customDeployer.deploy(artifact2);
72 | Assert.assertTrue(CustomDeployer.sample2Deployed);
73 | }
74 |
75 | @Test(dependsOnMethods = {"testDeploy"})
76 | public void testUpdate() throws CarbonDeploymentException {
77 | key = customDeployer.update(artifact);
78 | Assert.assertTrue(CustomDeployer.sample1Updated);
79 | key2 = customDeployer.update(artifact2);
80 | Assert.assertTrue(CustomDeployer.sample2Updated);
81 | }
82 |
83 | @Test(dependsOnMethods = {"testUpdate"})
84 | public void testUnDeploy() throws CarbonDeploymentException {
85 | customDeployer.undeploy(key);
86 | Assert.assertFalse(CustomDeployer.sample1Deployed);
87 | customDeployer.undeploy(key2);
88 | Assert.assertFalse(CustomDeployer.sample2Deployed);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/test/java/org/wso2/carbon/deployment/notifier/DeployerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier;
17 |
18 | import org.testng.Assert;
19 | import org.testng.annotations.BeforeTest;
20 | import org.testng.annotations.Test;
21 | import org.wso2.carbon.deployment.engine.Artifact;
22 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
23 | import org.wso2.carbon.deployment.notifier.deployers.CustomDeployer;
24 |
25 | import java.io.File;
26 | import java.util.HashMap;
27 |
28 | /**
29 | * Deployer Test class.
30 | *
31 | * @since 5.0.0
32 | */
33 | public class DeployerTest extends BaseTest {
34 | private static final String DEPLOYER_REPO = "carbon-repo" + File.separator + "text-files";
35 | private static final String RUNTIME_DEPLOYER_REPO = "deployment" + File.separator + "text-files";
36 | private CustomDeployer customDeployer;
37 | private Artifact artifact;
38 | private Artifact artifact2;
39 | private String key;
40 | private String key2;
41 | /**
42 | * @param testName
43 | */
44 | public DeployerTest(String testName) {
45 | super(testName);
46 | }
47 |
48 | @BeforeTest
49 | public void setup() throws CarbonDeploymentException {
50 | customDeployer = new CustomDeployer();
51 | customDeployer.init();
52 | artifact = new Artifact(new File(getTestResourceFile(DEPLOYER_REPO).getAbsolutePath()
53 | + File.separator + "sample1.txt"));
54 | artifact.setVersion("1.0.0");
55 | artifact.setProperties(new HashMap<>());
56 | artifact2 = new Artifact(new File(getTestResourceFile(RUNTIME_DEPLOYER_REPO).getAbsolutePath()
57 | + File.separator + "sample2.txt"));
58 | artifact2.setVersion("1.0.0");
59 | artifact2.setProperties(new HashMap<>());
60 | }
61 |
62 | @Test
63 | public void testDeployerInitialization() {
64 | Assert.assertTrue(CustomDeployer.initCalled);
65 | }
66 |
67 | @Test(dependsOnMethods = {"testDeployerInitialization"})
68 | public void testDeploy() throws CarbonDeploymentException {
69 | key = customDeployer.deploy(artifact);
70 | Assert.assertTrue(CustomDeployer.sample1Deployed);
71 | key2 = customDeployer.deploy(artifact2);
72 | Assert.assertTrue(CustomDeployer.sample2Deployed);
73 | }
74 |
75 | @Test(dependsOnMethods = {"testDeploy"})
76 | public void testUpdate() throws CarbonDeploymentException {
77 | key = customDeployer.update(artifact);
78 | Assert.assertTrue(CustomDeployer.sample1Updated);
79 | key2 = customDeployer.update(artifact2);
80 | Assert.assertTrue(CustomDeployer.sample2Updated);
81 | }
82 |
83 | @Test(dependsOnMethods = {"testUpdate"})
84 | public void testUnDeploy() throws CarbonDeploymentException {
85 | customDeployer.undeploy(key);
86 | Assert.assertFalse(CustomDeployer.sample1Deployed);
87 | customDeployer.undeploy(key2);
88 | Assert.assertFalse(CustomDeployer.sample2Deployed);
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/main/java/org/wso2/carbon/deployment/engine/LifecycleEvent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3 | *
4 | * WSO2 Inc. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 | package org.wso2.carbon.deployment.engine;
19 |
20 | import java.util.Date;
21 | import java.util.Optional;
22 | import java.util.Properties;
23 |
24 | /**
25 | * The deployment lifecycle of artifacts. An instance of this is passed
26 | * as part of the lifecycle event into the lifecycle listeners.
27 | *
28 | * @since 5.1.0
29 | *
30 | */
31 | public class LifecycleEvent {
32 |
33 | /**
34 | * Represents the lifecycle state of the artifacts.
35 | *
36 | */
37 | public enum STATE {
38 | BEFORE_START_EVENT,
39 | AFTER_START_EVENT,
40 | BEFORE_UPDATE_EVENT,
41 | AFTER_UPDATE_EVENT,
42 | BEFORE_STOP_EVENT,
43 | AFTER_STOP_EVENT
44 | }
45 |
46 | /**
47 | * The current artifact deployment/undeployment result.
48 | * If the artifact deployed/undeployed without any errors,
49 | * then the state will be successful denoting a successful artifact
50 | * deployment.
51 | *
52 | */
53 | public enum RESULT {
54 | SUCCESSFUL,
55 | FAILED
56 | }
57 |
58 | private STATE state;
59 |
60 | private Artifact artifact;
61 |
62 | private Date timestamp;
63 | private RESULT deploymentResult;
64 | private String traceContent;
65 |
66 | public Properties properties = new Properties();
67 |
68 | public LifecycleEvent(Artifact artifact, Date date, STATE state) {
69 | this.artifact = artifact;
70 | //assume a successful deployment initially.
71 | this.deploymentResult = RESULT.SUCCESSFUL;
72 | this.timestamp = Optional.ofNullable(date).map(tstamp -> new Date(date.getTime())).orElse(new Date());
73 | this.state = state;
74 | }
75 |
76 | public STATE getState() {
77 | return state;
78 | }
79 |
80 | public void setState(STATE state) {
81 | this.state = state;
82 | }
83 |
84 | public Artifact getArtifact() {
85 | return artifact;
86 | }
87 |
88 | public void setArtifact(Artifact artifact) {
89 | this.artifact = artifact;
90 | }
91 |
92 | public Date getTimestamp() {
93 | return new Date(Optional.of(timestamp).get().getTime());
94 | }
95 |
96 | public void setTimestamp(Date timestamp) {
97 | this.timestamp = new Date(timestamp.getTime());
98 | }
99 |
100 | public RESULT getDeploymentResult() {
101 | return deploymentResult;
102 | }
103 |
104 | public void setDeploymentResult(RESULT deploymentResult) {
105 | this.deploymentResult = deploymentResult;
106 | }
107 |
108 | public String getTraceContent() {
109 | return traceContent;
110 | }
111 |
112 | public void setTraceContent(String traceContent) {
113 | this.traceContent = traceContent;
114 | }
115 |
116 | public Properties getProperties() {
117 | return properties;
118 | }
119 |
120 | public void setProperties(Properties properties) {
121 | this.properties = properties;
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/main/java/org/wso2/carbon/deployment/engine/DeploymentService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine;
17 |
18 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
19 |
20 | /**
21 | * User level API's for consuming DeploymentEngine functionality.
22 | * This will be registered as an OSGI service so that users can reference this in their component.
23 | *
24 | * If the given artifact type is not recognized at DeploymentEngine level or there is no deployer
25 | * associated with the give artifact type, then these API's will result in unknown artifact type
26 | * error.
27 | *
28 | * @since 5.0.0
29 | */
30 |
31 | public interface DeploymentService {
32 | /**
33 | * User can call this method externally to deploy an artifact by giving the artifact deployment
34 | * directory and the path.
35 | *
36 | * @param artifactPath path where the artifact resides. This has to be the full qualified path
37 | * of the artifact
38 | * @param artifactType the type of the artifact going to be dpeloyed
39 | * Eg : webapp, dataservice, sequence
40 | * @throws CarbonDeploymentException - on error while trying deploy the given artifact info
41 | * @see ArtifactType
42 | */
43 | void deploy(String artifactPath, ArtifactType artifactType) throws CarbonDeploymentException;
44 |
45 | /**
46 | * When you want to undeploy an artifact, this method can be called by giving the key,
47 | * which uniquely identifies an artifact in a runtime and the artifact deployment
48 | * directory.
49 | *
50 | * @param key artifact key to uniquely identify an artifact in a runtime
51 | * Eg: for webapps this can be webapp context such as /foo , /bar, etc
52 | * for service this can be service name such as EchoService, VersionService, etc
53 | * @param artifactType the type of the artifact going to be deployed
54 | * Eg : webapp, dataservice, sequence
55 | * @throws CarbonDeploymentException - on error while trying undeploy the given artifact info
56 | * @see ArtifactType
57 | */
58 | void undeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException;
59 |
60 | /**
61 | * When you want to redeploy/update an artifact, this method can be called by giving the key,
62 | * which uniquely identifies an artifact in a runtime and the artifact deployment
63 | * directory.
64 | *
65 | * @param key artifact key to uniquely identify an artifact in a runtime
66 | * Eg: for webapps this can be webapp context such as /foo , /bar, etc
67 | * for service this can be service name such as EchoService, VersionService, etc
68 | * @param artifactType the type of the artifact going to be deployed
69 | * Eg : webapp, dataservice, sequence
70 | * @throws CarbonDeploymentException - on error while trying redeploy the given artifact info
71 | * @see ArtifactType
72 | */
73 | void redeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException;
74 | }
75 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/main/java/org/wso2/carbon/deployment/engine/internal/DataHolder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine.internal;
17 |
18 | import org.osgi.framework.BundleContext;
19 | import org.wso2.carbon.config.provider.ConfigProvider;
20 | import org.wso2.carbon.deployment.engine.config.DeploymentConfiguration;
21 | import org.wso2.carbon.kernel.CarbonRuntime;
22 |
23 | /**
24 | * Carbon kernel DataHolder.
25 | *
26 | * @since 5.0.0
27 | */
28 | public class DataHolder {
29 | private static DataHolder instance = new DataHolder();
30 | private BundleContext bundleContext;
31 |
32 | private CarbonRuntime carbonRuntime;
33 | private ConfigProvider configProvider;
34 | private DeploymentConfiguration deploymentConfiguration;
35 |
36 | public static DataHolder getInstance() {
37 | return instance;
38 | }
39 |
40 | public BundleContext getBundleContext() {
41 | return bundleContext;
42 | }
43 |
44 | public void setBundleContext(BundleContext bundleContext) {
45 | this.bundleContext = bundleContext;
46 | }
47 |
48 | /**
49 | * This method used within this bundle (carbon.core) scope to get the currently held carbonRuntime service instance
50 | * within this holder.
51 | *
52 | * @return this will return the carbonRuntime service instance.
53 | */
54 | public CarbonRuntime getCarbonRuntime() {
55 | return carbonRuntime;
56 | }
57 |
58 | /**
59 | * This method is called by the relevant service component that acquires the carbonRuntime service
60 | * instance and will be stored for future look-ups.
61 | *
62 | * @param carbonRuntime the carbonRuntime to be stored with this holder
63 | */
64 | public void setCarbonRuntime(CarbonRuntime carbonRuntime) {
65 | this.carbonRuntime = carbonRuntime;
66 | }
67 |
68 | /**
69 | * This method is called to set the config provider service.
70 | *
71 | * @param configProvider the configProvider to be stored with this holder.
72 | */
73 | public void setConfigProvider(ConfigProvider configProvider) {
74 |
75 | this.configProvider = configProvider;
76 | }
77 |
78 | /**
79 | * This method is used to get the config provider service.
80 | *
81 | * @return this will return the configProvider service instance.
82 | */
83 | public ConfigProvider getConfigProvider() {
84 | return configProvider;
85 | }
86 |
87 | /**
88 | * This method is used to get the deploymentConfig.
89 | *
90 | * @return this will return the deploymentConfig instance.
91 | */
92 | public DeploymentConfiguration getDeploymentConfiguration() {
93 | return deploymentConfiguration;
94 | }
95 |
96 | /**
97 | * This method is called to set the deploymentConfig object.
98 | *
99 | * @param deploymentConfiguration the deploymentConfig to be stored with this holder.
100 | */
101 | public void setDeploymentConfiguration(DeploymentConfiguration deploymentConfiguration) {
102 | this.deploymentConfiguration = deploymentConfiguration;
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/test/java/org/wso2/carbon/deployment/engine/service/CustomDeploymentService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine.service;
17 |
18 | import org.wso2.carbon.deployment.engine.Artifact;
19 | import org.wso2.carbon.deployment.engine.ArtifactType;
20 | import org.wso2.carbon.deployment.engine.Deployer;
21 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
22 | import org.wso2.carbon.deployment.engine.internal.CarbonDeploymentService;
23 | import org.wso2.carbon.deployment.engine.internal.DeploymentEngine;
24 |
25 | import java.io.File;
26 | import java.util.ArrayList;
27 |
28 | /**
29 | * Custom Deployment Service class.
30 | *
31 | * @since 5.0.0
32 | */
33 | public class CustomDeploymentService extends CarbonDeploymentService {
34 |
35 | private DeploymentEngine deploymentEngine;
36 |
37 | public CustomDeploymentService(DeploymentEngine deploymentEngine) {
38 | super(deploymentEngine);
39 | this.deploymentEngine = deploymentEngine;
40 | }
41 |
42 | @Override
43 | public void deploy(String artifactPath, ArtifactType artifactType)
44 | throws CarbonDeploymentException {
45 | try {
46 | super.deploy("fake/path", artifactType);
47 | } catch (CarbonDeploymentException e) {
48 | //ignore
49 | }
50 | Artifact artifact = new Artifact(new File(artifactPath));
51 | artifact.setType(artifactType);
52 | ArrayList artifactList = new ArrayList<>();
53 | artifactList.add(artifact);
54 | Deployer deployer = deploymentEngine.getDeployer(artifactType);
55 |
56 | if (deployer == null) {
57 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
58 | }
59 | deploymentEngine.deployArtifacts(artifactList);
60 | }
61 |
62 | @Override
63 | public void undeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException {
64 | try {
65 | super.undeploy("fake.key", artifactType);
66 | } catch (CarbonDeploymentException e) {
67 | // ignore
68 | }
69 | Deployer deployer = deploymentEngine.getDeployer(artifactType);
70 |
71 | if (deployer == null) {
72 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
73 | }
74 | deployer.undeploy(key);
75 | }
76 |
77 | @Override
78 | public void redeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException {
79 |
80 | try {
81 | super.redeploy("fake.key", artifactType);
82 | } catch (CarbonDeploymentException e) {
83 | // ignore
84 | }
85 | Deployer deployer = deploymentEngine.getDeployer(artifactType);
86 | if (deployer == null) {
87 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
88 | }
89 |
90 | Artifact deployedArtifact = deploymentEngine.getDeployedArtifact(artifactType, key);
91 |
92 | if (deployedArtifact == null) {
93 | throw new CarbonDeploymentException("Cannot find artifact with the key : " + key);
94 | }
95 |
96 | deployer.update(deployedArtifact);
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/test/java/org/wso2/carbon/deployment/notifier/service/CustomDeploymentService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier.service;
17 |
18 | import org.wso2.carbon.deployment.engine.Artifact;
19 | import org.wso2.carbon.deployment.engine.ArtifactType;
20 | import org.wso2.carbon.deployment.engine.Deployer;
21 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
22 | import org.wso2.carbon.deployment.engine.internal.CarbonDeploymentService;
23 | import org.wso2.carbon.deployment.engine.internal.DeploymentEngine;
24 |
25 | import java.io.File;
26 | import java.util.ArrayList;
27 |
28 | /**
29 | * Custom Deployment Service class.
30 | *
31 | * @since 5.0.0
32 | */
33 | public class CustomDeploymentService extends CarbonDeploymentService {
34 |
35 | private DeploymentEngine deploymentEngine;
36 |
37 | public CustomDeploymentService(DeploymentEngine deploymentEngine) {
38 | super(deploymentEngine);
39 | this.deploymentEngine = deploymentEngine;
40 | }
41 |
42 | @Override
43 | public void deploy(String artifactPath, ArtifactType artifactType)
44 | throws CarbonDeploymentException {
45 | try {
46 | super.deploy("fake/path", artifactType);
47 | } catch (CarbonDeploymentException e) {
48 | //ignore
49 | }
50 | Artifact artifact = new Artifact(new File(artifactPath));
51 | artifact.setType(artifactType);
52 | ArrayList artifactList = new ArrayList<>();
53 | artifactList.add(artifact);
54 | Deployer deployer = deploymentEngine.getDeployer(artifactType);
55 |
56 | if (deployer == null) {
57 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
58 | }
59 | deploymentEngine.deployArtifacts(artifactList);
60 | }
61 |
62 | @Override
63 | public void undeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException {
64 | try {
65 | super.undeploy("fake.key", artifactType);
66 | } catch (CarbonDeploymentException e) {
67 | // ignore
68 | }
69 | Deployer deployer = deploymentEngine.getDeployer(artifactType);
70 |
71 | if (deployer == null) {
72 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
73 | }
74 | deployer.undeploy(key);
75 | }
76 |
77 | @Override
78 | public void redeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException {
79 |
80 | try {
81 | super.redeploy("fake.key", artifactType);
82 | } catch (CarbonDeploymentException e) {
83 | // ignore
84 | }
85 | Deployer deployer = deploymentEngine.getDeployer(artifactType);
86 | if (deployer == null) {
87 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
88 | }
89 |
90 | Artifact deployedArtifact = deploymentEngine.getDeployedArtifact(artifactType, key);
91 |
92 | if (deployedArtifact == null) {
93 | throw new CarbonDeploymentException("Cannot find artifact with the key : " + key);
94 | }
95 |
96 | deployer.update(deployedArtifact);
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Carbon Deployment
2 |
3 | ---
4 |
5 | | Branch | Build Status |
6 | | :------------ |:-------------
7 | | master | [](https://wso2.org/jenkins/job/carbon-deployment) |
8 | | release-4.2.x | [](https://wso2.org/jenkins/job/carbon-deployment_release-4.2.x/) |
9 |
10 |
11 | ---
12 | Carbon 5.x artifact deployment framework - This is an extensible framework where Carbon developers get to incorporate and write their own artifact deployers to deploy its artifacts into a Carbon 5 based product.
13 |
14 | This provides following interfaces -
15 | * `DeploymentService` -
16 |
17 | User level API for consuming `DeploymentEngine` functionality. An implementation of this is registered as an OSGI service which can be used by developers to deploy/undeploy/redeploy their artifacts.
18 |
19 | * `Deployer` -
20 |
21 | This interface is used to provide the deployment mechanism in carbon for custom artifacts, where you can write your own Deployer to process a particular ArtifactType. Developers need to develop implementations based on this interface, and register it as an OSGi service (using the Deployer as the interface) for the DeploymentEngine to find.
22 |
23 | * `LifecycleListener` -
24 |
25 | This interface can be used to write your own lifecycle listeners to listen on artifact deployment events. The implementation should be registered as an OSGi service with LifecycleListener as the interface. This interface receives following events.
26 |
27 | ```bash
28 | BEFORE_START_EVENT
29 | AFTER_START_EVENT
30 | BEFORE_STOP_EVENT
31 | AFTER_STOP_EVENT
32 | ```
33 |
34 | ## Download
35 |
36 | Use Maven snippet:
37 | ````xml
38 |
39 | org.wso2.carbon.deployment
40 | org.wso2.carbon.deployment.engine
41 | ${carbon.deployment.version}
42 |
43 |
44 |
45 | org.wso2.carbon.deployment
46 | org.wso2.carbon.deployment.notifier
47 | ${carbon.deployment.version}
48 |
49 | ````
50 |
51 | ### Snapshot Releases
52 |
53 | Use following Maven repository for snapshot versions of Carbon Deployment.
54 |
55 | ````xml
56 |
57 | wso2.snapshots
58 | WSO2 Snapshot Repository
59 | http://maven.wso2.org/nexus/content/repositories/snapshots/
60 |
61 | true
62 | daily
63 |
64 |
65 | false
66 |
67 |
68 | ````
69 |
70 | ### Released Versions
71 |
72 | Use following Maven repository for released stable versions of Carbon Deployment.
73 |
74 | ````xml
75 |
76 | wso2.releases
77 | WSO2 Releases Repository
78 | http://maven.wso2.org/nexus/content/repositories/releases/
79 |
80 | true
81 | daily
82 | ignore
83 |
84 |
85 | ````
86 | ## Building From Source
87 |
88 | Clone this repository first (`git clone https://github.com/wso2/carbon-deployment.git`) and use Apache Maven to build `mvn clean install`.
89 |
90 |
91 | ## How to Contribute
92 | * Please report issues at [Carbon JIRA] (https://wso2.org/jira/browse/CARBON).
93 | * Send your pull requests to [master branch] (https://github.com/wso2/carbon-deployment/tree/master)
94 |
95 | ## License
96 |
97 | Carbon Deployment is available under the Apache 2 License.
98 |
99 | ## Contact us
100 | WSO2 Carbon developers can be contacted via the mailing lists:
101 |
102 | * Carbon Developers List : dev@wso2.org
103 | * Carbon Architecture List : architecture@wso2.org
104 |
105 | ## Copyright
106 |
107 | Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
108 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/test/java/org/wso2/carbon/deployment/engine/DeploymentServiceTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine;
17 |
18 | import org.testng.Assert;
19 | import org.testng.annotations.AfterTest;
20 | import org.testng.annotations.BeforeTest;
21 | import org.testng.annotations.Test;
22 | import org.wso2.carbon.deployment.engine.deployers.CustomDeployer;
23 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
24 | import org.wso2.carbon.deployment.engine.exception.DeployerRegistrationException;
25 | import org.wso2.carbon.deployment.engine.exception.DeploymentEngineException;
26 | import org.wso2.carbon.deployment.engine.internal.DeploymentEngine;
27 | import org.wso2.carbon.deployment.engine.service.CustomDeploymentService;
28 | import org.wso2.carbon.utils.FileUtils;
29 |
30 | import java.io.File;
31 | import java.io.IOException;
32 |
33 | /**
34 | * Deployment Service Test class.
35 | *
36 | * @since 5.0.0
37 | */
38 | public class DeploymentServiceTest extends BaseTest {
39 |
40 | private static final String CARBON_REPO = "carbon-repo";
41 | private static final String RUNTIME_REPO = "deployment";
42 | private static final String DEPLOYER_REPO = "carbon-repo" + File.separator + "text-files";
43 | private CustomDeploymentService deploymentService;
44 | private DeploymentEngine deploymentEngine;
45 | private CustomDeployer customDeployer;
46 | private String artifactPath;
47 |
48 | /**
49 | * @param testName
50 | */
51 | public DeploymentServiceTest(String testName) {
52 | super(testName);
53 | }
54 |
55 | @BeforeTest
56 | public void setup() throws DeploymentEngineException, DeployerRegistrationException {
57 | customDeployer = new CustomDeployer();
58 | artifactPath = getTestResourceFile(DEPLOYER_REPO).getAbsolutePath()
59 | + File.separator + "sample1.txt";
60 | deploymentEngine = new DeploymentEngine();
61 | deploymentEngine.start(getTestResourceFile(CARBON_REPO).getAbsolutePath(),
62 | getTestResourceFile(DEPLOYER_REPO).getAbsolutePath());
63 | deploymentEngine.registerDeployer(customDeployer);
64 | }
65 |
66 | @Test
67 | public void testDeploymentService() {
68 | deploymentService = new CustomDeploymentService(deploymentEngine);
69 | }
70 |
71 | @Test(dependsOnMethods = {"testDeploymentService"})
72 | public void testDeploy() throws CarbonDeploymentException {
73 | deploymentService.deploy(artifactPath, customDeployer.getArtifactType());
74 | Assert.assertTrue(CustomDeployer.sample1Deployed);
75 | }
76 |
77 | @Test(dependsOnMethods = {"testDeploy"})
78 | public void testUpdate() throws CarbonDeploymentException {
79 | deploymentService.redeploy(new File(artifactPath).getName(),
80 | customDeployer.getArtifactType());
81 | Assert.assertTrue(CustomDeployer.sample1Updated);
82 | }
83 |
84 | @Test(dependsOnMethods = {"testUpdate"})
85 | public void testUndeploy() throws CarbonDeploymentException {
86 | deploymentService.undeploy(new File(artifactPath).getName(),
87 | customDeployer.getArtifactType());
88 | Assert.assertFalse(CustomDeployer.sample1Deployed);
89 | }
90 |
91 | @AfterTest
92 | public void cleanupTempfile() throws IOException {
93 | FileUtils.deleteDir(new File(getTestResourceFile(CARBON_REPO).getAbsolutePath() +
94 | File.separator + "file:text-files"));
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/test/java/org/wso2/carbon/deployment/notifier/deployers/FaultyDeployer2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier.deployers;
17 |
18 | import org.slf4j.Logger;
19 | import org.slf4j.LoggerFactory;
20 | import org.wso2.carbon.deployment.engine.Artifact;
21 | import org.wso2.carbon.deployment.engine.ArtifactType;
22 | import org.wso2.carbon.deployment.engine.Deployer;
23 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
24 |
25 | import java.net.MalformedURLException;
26 | import java.net.URL;
27 |
28 | /**
29 | * A Faulty Deployer class to test deployment.
30 | *
31 | * @since 5.0.0
32 | */
33 | public class FaultyDeployer2 implements Deployer {
34 | private static final Logger logger = LoggerFactory.getLogger(FaultyDeployer2.class);
35 | /**
36 | * Has init() been called?.
37 | */
38 | public static boolean initCalled;
39 | /**
40 | * Set to true if "XML1" has been deployed.
41 | */
42 | public static boolean sample1Deployed;
43 | /**
44 | * Set to true if "XML2" has been deployed.
45 | */
46 | public static boolean sample2Deployed;
47 | /**
48 | * Set to true if "XML1" has been updated.
49 | */
50 | public static boolean sample1Updated;
51 | /**
52 | * Set to true if "XML1" has been updated.
53 | */
54 | public static boolean sample2Updated;
55 |
56 | private URL directoryLocation;
57 | private ArtifactType artifactType;
58 |
59 | public FaultyDeployer2() {
60 | artifactType = new ArtifactType<>("txt");
61 | try {
62 | directoryLocation = new URL("file:text-files");
63 | } catch (MalformedURLException e) {
64 | logger.error("Error while initializing directoryLocation", e);
65 | }
66 | }
67 |
68 | public void init() {
69 | logger.info("Initializing Deployer");
70 | initCalled = true;
71 | }
72 |
73 | public String deploy(Artifact artifact) throws CarbonDeploymentException {
74 | logger.info("Deploying : " + artifact.getName());
75 | if (artifact.getName() != null && artifact.getName().contains("sample1.txt")) {
76 | sample1Deployed = false;
77 | } else if (artifact.getName() != null && artifact.getName().contains("sample2.txt")) {
78 | sample2Deployed = false;
79 | }
80 | return null;
81 | }
82 |
83 | public void undeploy(Object key) throws CarbonDeploymentException {
84 | logger.info("Undeploying : " + key);
85 | throw new CarbonDeploymentException("Error while Un Deploying : " + key);
86 | }
87 |
88 | public String update(Artifact artifact) throws CarbonDeploymentException {
89 | logger.info("Updating : " + artifact.getName());
90 | if (artifact.getName() != null && artifact.getName().contains("sample1.txt")) {
91 | sample1Updated = false;
92 | } else if (artifact.getName().contains("sample2.txt")) {
93 | sample2Updated = false;
94 | }
95 | return null;
96 | }
97 |
98 |
99 | public URL getLocation() {
100 | return directoryLocation;
101 | }
102 |
103 | public void setLocation(URL directoryLocation) {
104 | this.directoryLocation = directoryLocation;
105 | }
106 |
107 | public ArtifactType getArtifactType() {
108 | return artifactType;
109 | }
110 |
111 | public void setArtifactType(ArtifactType artifactType) {
112 | this.artifactType = artifactType;
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/test/java/org/wso2/carbon/deployment/notifier/deployers/FaultyDeployer1.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier.deployers;
17 |
18 | import org.slf4j.Logger;
19 | import org.slf4j.LoggerFactory;
20 | import org.wso2.carbon.deployment.engine.Artifact;
21 | import org.wso2.carbon.deployment.engine.ArtifactType;
22 | import org.wso2.carbon.deployment.engine.Deployer;
23 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
24 |
25 | import java.net.MalformedURLException;
26 | import java.net.URL;
27 |
28 | /**
29 | * A Faulty Deployer class to test deployment.
30 | *
31 | * @since 5.0.0
32 | */
33 | public class FaultyDeployer1 implements Deployer {
34 | private static final Logger logger = LoggerFactory.getLogger(FaultyDeployer1.class);
35 | /**
36 | * Has init() been called?.
37 | */
38 | public static boolean initCalled;
39 | /**
40 | * Set to true if "XML1" has been deployed.
41 | */
42 | public static boolean sample1Deployed;
43 | /**
44 | * Set to true if "XML2" has been deployed.
45 | */
46 | public static boolean sample2Deployed;
47 | /**
48 | * Set to true if "XML1" has been updated.
49 | */
50 | public static boolean sample1Updated;
51 | /**
52 | * Set to true if "XML2" has been updated.
53 | */
54 | public static boolean sample2Updated;
55 |
56 | private URL directoryLocation;
57 | private ArtifactType artifactType;
58 |
59 | public FaultyDeployer1() {
60 | artifactType = new ArtifactType<>("txt");
61 | try {
62 | directoryLocation = new URL("file:text-files");
63 | } catch (MalformedURLException e) {
64 | logger.error("Error while initializing directoryLocation", e);
65 | }
66 | }
67 |
68 | public void init() {
69 | logger.info("Initializing Deployer");
70 | initCalled = true;
71 | }
72 |
73 | public String deploy(Artifact artifact) throws CarbonDeploymentException {
74 | logger.info("Deploying : " + artifact.getName());
75 | if (artifact.getName() != null && artifact.getName().contains("sample1.txt")) {
76 | sample1Deployed = false;
77 | } else if (artifact.getName() != null && artifact.getName().contains("sample2.txt")) {
78 | sample2Deployed = false;
79 | }
80 | throw new CarbonDeploymentException("Error while deploying : " + artifact.getName());
81 | }
82 |
83 | public void undeploy(Object key) throws CarbonDeploymentException {
84 | logger.info("Undeploying : " + key);
85 | throw new CarbonDeploymentException("Error while Un Deploying : " + key);
86 | }
87 |
88 | public String update(Artifact artifact) throws CarbonDeploymentException {
89 | logger.info("Updating : " + artifact.getName());
90 | if (artifact.getName() != null && artifact.getName().contains("sample1.txt")) {
91 | sample1Updated = true;
92 | } else if (artifact.getName() != null && artifact.getName().contains("sample2.txt")) {
93 | sample2Updated = true;
94 | }
95 | return artifact.getName();
96 | }
97 |
98 |
99 | public URL getLocation() {
100 | return directoryLocation;
101 | }
102 |
103 | public void setLocation(URL directoryLocation) {
104 | this.directoryLocation = directoryLocation;
105 | }
106 |
107 | public ArtifactType getArtifactType() {
108 | return artifactType;
109 | }
110 |
111 | public void setArtifactType(ArtifactType artifactType) {
112 | this.artifactType = artifactType;
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/tests/osgi-tests/src/test/java/org/wso2/carbon/deployment/engine/osgi/CustomDeployer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine.osgi;
17 |
18 | import org.slf4j.Logger;
19 | import org.slf4j.LoggerFactory;
20 | import org.wso2.carbon.deployment.engine.Artifact;
21 | import org.wso2.carbon.deployment.engine.ArtifactType;
22 | import org.wso2.carbon.deployment.engine.Deployer;
23 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
24 |
25 | import java.io.File;
26 | import java.io.FileInputStream;
27 | import java.io.IOException;
28 | import java.net.MalformedURLException;
29 | import java.net.URL;
30 |
31 | /**
32 | * Custom Deployer class to test deployment engine OSGi test case.
33 | *
34 | * @since 5.0.0
35 | */
36 | public class CustomDeployer implements Deployer {
37 | private static final Logger logger = LoggerFactory.getLogger(CustomDeployer.class);
38 |
39 | private String directory = "carbon-repo/text-files";
40 | private URL directoryLocation;
41 | private ArtifactType artifactType;
42 | private String testDir = "src" + File.separator + "test" + File.separator + "resources" +
43 | File.separator + "carbon-repo" + File.separator + directory;
44 |
45 | public CustomDeployer() {
46 | artifactType = new ArtifactType<>("txt");
47 | try {
48 | directoryLocation = new URL("file:text-files");
49 | } catch (MalformedURLException e) {
50 | logger.error("Error while initializing directoryLocation", e);
51 | }
52 | }
53 |
54 | public void init() {
55 | logger.info("Initializing Deployer");
56 | }
57 |
58 | public String deploy(Artifact artifact) throws CarbonDeploymentException {
59 | logger.info("Deploying : " + artifact.getName());
60 | String key = null;
61 | try (FileInputStream fis = new FileInputStream(artifact.getFile())) {
62 | int x = fis.available();
63 | byte b[] = new byte[x];
64 | fis.read(b);
65 | String content = new String(b);
66 | if (content.contains("sample1")) {
67 | key = artifact.getName();
68 | }
69 | } catch (IOException e) {
70 | throw new CarbonDeploymentException("Error while deploying : " + artifact.getName(), e);
71 | }
72 | return key;
73 | }
74 |
75 | public void undeploy(Object key) throws CarbonDeploymentException {
76 | if (!(key instanceof String)) {
77 | throw new CarbonDeploymentException("Error while Un Deploying : " + key +
78 | "is not a String value");
79 | }
80 | logger.info("Undeploying : " + key);
81 | File fileToUndeploy = new File(testDir + File.separator + key);
82 | logger.info("File to undeploy : " + fileToUndeploy.getAbsolutePath());
83 | try (FileInputStream fis = new FileInputStream(fileToUndeploy)) {
84 | int x = fis.available();
85 | byte b[] = new byte[x];
86 | fis.read(b);
87 | String content = new String(b);
88 | if (content.contains("sample1")) {
89 | logger.info("Undeployed : " + key);
90 | }
91 | } catch (IOException e) {
92 | throw new CarbonDeploymentException("Error while Un Deploying : " + key, e);
93 | }
94 | }
95 |
96 | public String update(Artifact artifact) throws CarbonDeploymentException {
97 | logger.info("Updating : " + artifact.getName());
98 | return artifact.getName();
99 | }
100 |
101 |
102 | public URL getLocation() {
103 | return directoryLocation;
104 | }
105 |
106 | public void setLocation(URL directoryLocation) {
107 | this.directoryLocation = directoryLocation;
108 | }
109 |
110 | public ArtifactType getArtifactType() {
111 | return artifactType;
112 | }
113 |
114 | public void setArtifactType(ArtifactType artifactType) {
115 | this.artifactType = artifactType;
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/features/org.wso2.carbon.deployment.notifier.feature/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 | org.wso2.carbon.deployment
21 | org.wso2.carbon.deployment.parent
22 | 5.1.8-SNAPSHOT
23 | ../../pom.xml
24 |
25 |
26 | 4.0.0
27 | org.wso2.carbon.deployment.notifier.feature
28 | 5.1.8-SNAPSHOT
29 | carbon-feature
30 |
31 | WSO2 Carbon Deployment Notifier - Feature
32 | http://wso2.org
33 | This feature contains the core bundles required by the Deployment feature
34 |
35 |
36 |
37 | org.wso2.carbon.deployment
38 | org.wso2.carbon.deployment.notifier
39 |
40 |
41 | org.apache.geronimo.specs
42 | geronimo-jms_1.1_spec
43 |
44 |
45 | commons-pool.wso2
46 | commons-pool
47 |
48 |
49 |
50 |
51 |
52 |
53 | resources
54 |
55 |
56 | src/main/resources
57 |
58 |
59 |
60 |
61 | org.wso2.carbon.maven
62 | carbon-feature-plugin
63 | true
64 |
65 |
66 | 1-p2-feature-generation
67 |
68 | generate
69 |
70 |
71 | ../etc/feature.properties
72 |
73 |
74 | org.wso2.carbon.p2.category.type
75 | server
76 |
77 |
78 | org.eclipse.equinox.p2.type.group
79 | false
80 |
81 |
82 |
83 |
84 | org.wso2.carbon.deployment.notifier
85 | ${carbon.deployment.version}
86 |
87 |
88 | org.apache.geronimo.specs.geronimo-jms_1.1_spec
89 | ${geronimo.jms.spec.version}
90 |
91 |
92 | commons-pool
93 | ${commons.pool.version}
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/main/java/org/wso2/carbon/deployment/notifier/internal/DeploymentNotificationMessage.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
3 | *
4 | * WSO2 Inc. licenses this file to you under the Apache License,
5 | * Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License.
7 | * You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing,
12 | * software distributed under the License is distributed on an
13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 | * KIND, either express or implied. See the License for the
15 | * specific language governing permissions and limitations
16 | * under the License.
17 | */
18 | package org.wso2.carbon.deployment.notifier.internal;
19 |
20 | import org.wso2.carbon.deployment.engine.Artifact;
21 | import org.wso2.carbon.deployment.engine.LifecycleEvent;
22 |
23 | import java.util.Date;
24 | import java.util.Optional;
25 | import java.util.Properties;
26 | import javax.xml.bind.annotation.XmlAccessType;
27 | import javax.xml.bind.annotation.XmlAccessorType;
28 | import javax.xml.bind.annotation.XmlElement;
29 | import javax.xml.bind.annotation.XmlRootElement;
30 |
31 | /**
32 | * The JAXB POJO that is used to send the artifact deployment
33 | * status, usually to a JMS topic.
34 | *
35 | * @since 5.0.0
36 | *
37 | */
38 | @XmlRootElement
39 | @XmlAccessorType(XmlAccessType.FIELD)
40 | public class DeploymentNotificationMessage {
41 |
42 | @XmlElement
43 | private Object artifactKey;
44 |
45 | @XmlElement
46 | private String artifactType;
47 |
48 | @XmlElement
49 | private String serverId;
50 |
51 | @XmlElement
52 | private Date timestamp;
53 |
54 | @XmlElement
55 | private LifecycleEvent.RESULT currentDeploymentResult;
56 |
57 | @XmlElement
58 | private LifecycleEvent.STATE lifecycleState;
59 |
60 | @XmlElement
61 | private String message;
62 |
63 | @XmlElement
64 | public Properties properties;
65 |
66 | public DeploymentNotificationMessage(Artifact artifact) {
67 | this(artifact, null);
68 | }
69 |
70 | /**
71 | * The default constructor is required by JAXB
72 | * to do serialization.
73 | */
74 | public DeploymentNotificationMessage() {
75 | }
76 |
77 | /**
78 | * Extracts info from the Artifact object.
79 | *
80 | * @param artifact The artifact that we need to extract info from
81 | */
82 | public DeploymentNotificationMessage(Artifact artifact, Date timestamp) {
83 | this.artifactKey = artifact.getKey();
84 | this.artifactType = artifact.getType().get().toString();
85 | this.timestamp = Optional.ofNullable(timestamp).map(tstamp -> new Date(timestamp.getTime())).orElse(new Date());
86 | }
87 |
88 | public Object getArtifactKey() {
89 | return artifactKey;
90 | }
91 |
92 | public void setArtifactKey(Object artifactKey) {
93 | this.artifactKey = artifactKey;
94 | }
95 |
96 | public String getArtifactType() {
97 | return artifactType;
98 | }
99 |
100 | public void setArtifactType(String artifactType) {
101 | this.artifactType = artifactType;
102 | }
103 |
104 | public String getServerId() {
105 | return serverId;
106 | }
107 |
108 | public void setServerId(String host) {
109 | this.serverId = host;
110 | }
111 |
112 | public Date getTimestamp() {
113 | return new Date(timestamp.getTime());
114 | }
115 |
116 | public void setTimestamp(Date timestamp) {
117 | this.timestamp = new Date(timestamp.getTime());
118 | }
119 |
120 | public LifecycleEvent.STATE getLifecycleState() {
121 | return lifecycleState;
122 | }
123 |
124 | public void setLifecycleState(LifecycleEvent.STATE lifecycleState) {
125 | this.lifecycleState = lifecycleState;
126 | }
127 |
128 | public LifecycleEvent.RESULT getCurrentDeploymentResult() {
129 | return currentDeploymentResult;
130 | }
131 |
132 | public void setCurrentDeploymentResult(LifecycleEvent.RESULT currentDeploymentResult) {
133 | this.currentDeploymentResult = currentDeploymentResult;
134 | }
135 |
136 | public String getMessage() {
137 | return message;
138 | }
139 |
140 | public void setTraceContent(String message) {
141 | this.message = message;
142 | }
143 |
144 | public Properties getProperties() {
145 | return properties;
146 | }
147 |
148 | public void setProperties(Properties properties) {
149 | this.properties = properties;
150 | }
151 |
152 | @Override
153 | public String toString() {
154 | return String.format("%1$s - %2$s - %3$s ", artifactKey, artifactType, lifecycleState);
155 | }
156 |
157 | }
158 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/test/java/org/wso2/carbon/deployment/notifier/DeploymentServiceTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier;
17 |
18 | import org.testng.Assert;
19 | import org.testng.annotations.AfterTest;
20 | import org.testng.annotations.BeforeTest;
21 | import org.testng.annotations.Test;
22 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
23 | import org.wso2.carbon.deployment.engine.internal.DeploymentEngine;
24 | import org.wso2.carbon.deployment.notifier.deployers.CustomDeployer;
25 | import org.wso2.carbon.deployment.notifier.service.CustomDeploymentService;
26 | import org.wso2.carbon.utils.FileUtils;
27 |
28 | import java.io.File;
29 | import java.io.IOException;
30 |
31 | /**
32 | * Deployment Service Test class.
33 | *
34 | * @since 5.0.0
35 | */
36 | public class DeploymentServiceTest extends BaseTest {
37 |
38 | private static final String CARBON_REPO = "carbon-repo";
39 | private static final String RUNTIME_REPO = "deployment";
40 | private static final String DEPLOYER_REPO = CARBON_REPO + File.separator + "text-files";
41 | private static final String RUNTIME_DEPLOYER_REPO = RUNTIME_REPO + File.separator + "text-files";
42 | private CustomDeploymentService deploymentService;
43 | private DeploymentEngine deploymentEngine;
44 | private CustomDeployer customDeployer;
45 | private String artifactPath;
46 | private String artifactPath2;
47 |
48 |
49 | /**
50 | * @param testName
51 | */
52 | public DeploymentServiceTest(String testName) {
53 | super(testName);
54 | }
55 |
56 | @BeforeTest
57 | public void setup() throws Exception {
58 | customDeployer = new CustomDeployer();
59 | artifactPath = getTestResourceFile(DEPLOYER_REPO).getAbsolutePath()
60 | + File.separator + "sample1.txt";
61 | artifactPath2 = getTestResourceFile(RUNTIME_DEPLOYER_REPO).getAbsolutePath()
62 | + File.separator + "sample2.txt";
63 | deploymentEngine = new DeploymentEngine();
64 | deploymentEngine.start(getTestResourceFile(CARBON_REPO).getAbsolutePath(),
65 | getTestResourceFile(DEPLOYER_REPO).getAbsolutePath());
66 | deploymentEngine.registerDeployer(customDeployer);
67 | }
68 |
69 | @Test
70 | public void testDeploymentService() {
71 | deploymentService = new CustomDeploymentService(deploymentEngine);
72 | }
73 |
74 | @Test(dependsOnMethods = {"testDeploymentService"})
75 | public void testDeploy() throws CarbonDeploymentException {
76 | deploymentService.deploy(artifactPath, customDeployer.getArtifactType());
77 | Assert.assertTrue(CustomDeployer.sample1Deployed);
78 | deploymentService.deploy(artifactPath2, customDeployer.getArtifactType());
79 | Assert.assertTrue(CustomDeployer.sample2Deployed);
80 | }
81 |
82 | @Test(dependsOnMethods = {"testDeploy"})
83 | public void testUpdate() throws CarbonDeploymentException {
84 | deploymentService.redeploy(new File(artifactPath).getName(),
85 | customDeployer.getArtifactType());
86 | Assert.assertTrue(CustomDeployer.sample1Updated);
87 | deploymentService.redeploy(new File(artifactPath2).getName(),
88 | customDeployer.getArtifactType());
89 | Assert.assertTrue(CustomDeployer.sample2Updated);
90 | }
91 |
92 | @Test(dependsOnMethods = {"testUpdate"})
93 | public void testUndeploy() throws CarbonDeploymentException {
94 | deploymentService.undeploy(new File(artifactPath).getName(),
95 | customDeployer.getArtifactType());
96 | Assert.assertFalse(CustomDeployer.sample1Deployed);
97 | deploymentService.undeploy(new File(artifactPath2).getName(),
98 | customDeployer.getArtifactType());
99 | Assert.assertFalse(CustomDeployer.sample2Deployed);
100 | }
101 |
102 | @AfterTest
103 | public void cleanupTempfile() throws IOException {
104 | FileUtils.deleteDir(new File(getTestResourceFile(CARBON_REPO).getAbsolutePath() +
105 | File.separator + "file:text-files"));
106 | FileUtils.deleteDir(new File(getTestResourceFile(RUNTIME_REPO).getAbsolutePath() +
107 | File.separator + "file:text-files"));
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/features/org.wso2.carbon.deployment.engine.feature/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 | org.wso2.carbon.deployment
21 | org.wso2.carbon.deployment.parent
22 | 5.1.8-SNAPSHOT
23 | ../../pom.xml
24 |
25 |
26 | 4.0.0
27 | org.wso2.carbon.deployment.engine.feature
28 | 5.1.8-SNAPSHOT
29 | carbon-feature
30 |
31 | WSO2 Carbon Deployment Engine - Feature
32 | http://wso2.org
33 | This feature contains the core bundles required by the Deployment feature
34 |
35 |
36 |
37 | org.wso2.carbon.deployment
38 | org.wso2.carbon.deployment.engine
39 |
40 |
41 |
42 |
43 |
44 |
45 | resources
46 |
47 |
48 | src/main/resources
49 |
50 |
51 | ${project.build.directory}/docs/
52 |
53 |
54 |
55 |
56 | org.apache.maven.plugins
57 | maven-dependency-plugin
58 |
59 |
60 | unpack
61 | package
62 |
63 | unpack
64 |
65 |
66 |
67 |
68 | org.wso2.carbon.deployment
69 | org.wso2.carbon.deployment.engine
70 | ${carbon.deployment.version}
71 | bundle
72 | true
73 | ${project.build.directory}/docs
74 | config-docs/**
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | org.wso2.carbon.maven
83 | carbon-feature-plugin
84 | true
85 |
86 |
87 | 1-p2-feature-generation
88 |
89 | generate
90 |
91 |
92 | ../etc/feature.properties
93 |
94 |
95 | org.wso2.carbon.p2.category.type
96 | server
97 |
98 |
99 | org.eclipse.equinox.p2.type.group
100 | false
101 |
102 |
103 |
104 |
105 | org.wso2.carbon.deployment.engine
106 | ${carbon.deployment.version}
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/main/java/org/wso2/carbon/deployment/notifier/internal/DeploymentNotifierListenerComponent.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier.internal;
17 |
18 | import org.osgi.framework.BundleContext;
19 | import org.osgi.service.component.annotations.Activate;
20 | import org.osgi.service.component.annotations.Component;
21 | import org.osgi.service.component.annotations.Deactivate;
22 | import org.osgi.service.component.annotations.Reference;
23 | import org.osgi.service.component.annotations.ReferenceCardinality;
24 | import org.osgi.service.component.annotations.ReferencePolicy;
25 | import org.slf4j.Logger;
26 | import org.slf4j.LoggerFactory;
27 | import org.wso2.carbon.config.provider.ConfigProvider;
28 | import org.wso2.carbon.deployment.engine.LifecycleListener;
29 | import org.wso2.carbon.deployment.notifier.DeploymentNotifierLifecycleListener;
30 | import org.wso2.carbon.kernel.CarbonRuntime;
31 |
32 | /**
33 | * This service component is responsible for initializing the DeploymentEngine and listening for deployer registrations.
34 | *
35 | * @since 5.0.0
36 | */
37 | @Component(
38 | name = "DeploymentNotifierListenerComponent",
39 | immediate = true
40 | )
41 | @SuppressWarnings("unused")
42 | public class DeploymentNotifierListenerComponent {
43 | private static final Logger logger = LoggerFactory.getLogger(DeploymentNotifierListenerComponent.class);
44 |
45 | /**
46 | * This is the activation method of DeploymentNotifierListenerComponent. This will be called when its references are
47 | * satisfied.
48 | *
49 | * @param bundleContext the bundle context instance of the carbon core bundle used service registration, etc.
50 | * @throws Exception this will be thrown if an issue occurs while executing the activate method
51 | */
52 | @Activate
53 | public void start(BundleContext bundleContext) throws Exception {
54 | LifecycleListener deploymentCoordinationLifecycleListener = new DeploymentNotifierLifecycleListener();
55 | bundleContext.registerService(LifecycleListener.class, deploymentCoordinationLifecycleListener, null);
56 | logger.debug("Registered DeploymentNotifierLifecycleListener.");
57 | }
58 |
59 | /**
60 | * This is the deactivation method of DeploymentNotifierListenerComponent. This will be called when this component
61 | * is being stopped or references are satisfied during runtime.
62 | *
63 | * @throws Exception this will be thrown if an issue occurs while executing the de-activate method
64 | */
65 | @Deactivate
66 | public void stop() throws Exception {
67 | }
68 |
69 | /**
70 | * Get the CarbonRuntime service.
71 | * This is the bind method that gets called for CarbonRuntime service registration that satisfy the policy.
72 | *
73 | * @param carbonRuntime the CarbonRuntime service that is registered as a service.
74 | */
75 | @Reference(
76 | name = "carbon.runtime.service",
77 | service = CarbonRuntime.class,
78 | cardinality = ReferenceCardinality.MANDATORY,
79 | policy = ReferencePolicy.DYNAMIC,
80 | unbind = "unregisterCarbonRuntime"
81 | )
82 | protected void registerCarbonRuntime(CarbonRuntime carbonRuntime) {
83 | DataHolder.getInstance().setCarbonRuntime(carbonRuntime);
84 | }
85 |
86 | /**
87 | * This is the unbind method for the above reference that gets called for CarbonRuntime instance un-registrations.
88 | *
89 | * @param carbonRuntime the CarbonRuntime service that get unregistered.
90 | */
91 | protected void unregisterCarbonRuntime(CarbonRuntime carbonRuntime) {
92 | DataHolder.getInstance().setCarbonRuntime(null);
93 | }
94 |
95 | /**
96 | * Get the ConfigProvider service.
97 | * This is the bind method that gets called for ConfigProvider service registration that satisfy the policy.
98 | *
99 | * @param configProvider the ConfigProvider service that is registered as a service.
100 | */
101 | @Reference(
102 | name = "carbon.config.provider",
103 | service = ConfigProvider.class,
104 | cardinality = ReferenceCardinality.MANDATORY,
105 | policy = ReferencePolicy.DYNAMIC,
106 | unbind = "unregisterConfigProvider"
107 | )
108 | protected void registerConfigProvider(ConfigProvider configProvider) {
109 | DataHolder.getInstance().setConfigProvider(configProvider);
110 | }
111 |
112 | /**
113 | * This is the unbind method for the above reference that gets called for ConfigProvider instance un-registrations.
114 | *
115 | * @param configProvider the ConfigProvider service that get unregistered.
116 | */
117 | protected void unregisterConfigProvider(ConfigProvider configProvider) {
118 | DataHolder.getInstance().setConfigProvider(null);
119 | }
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/main/java/org/wso2/carbon/deployment/engine/Artifact.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine;
17 |
18 | import java.io.File;
19 | import java.util.HashMap;
20 | import java.util.Map;
21 |
22 | /**
23 | *
24 | * This class provide an abstraction level for the concept "artifact" in carbon.
25 | *
26 | *
27 | * An artifact can be considered as a thing to deploy in carbon.
28 | * Eg: webapp, service, jaggery-app, proxy-service, sequence, spi, endpoint
29 | *
30 | *
31 | * Each artifact is associated with a deployer, which can process and deploy it to the relevant
32 | * runtime configuration.
33 | *
34 | *
35 | * An artifact will have a unique identifier (key), which can be used for artifact identification
36 | * within a runtime.
37 | *
38 | * An artifact can have custom properties that are needed for relevant runtime environment.
39 | *
40 | * @since 5.0.0
41 | */
42 | public class Artifact {
43 |
44 | /**
45 | * The file associated with the artifact instance.
46 | */
47 | private File file;
48 |
49 | /**
50 | * Keeps track of the last modified time of this artifact.
51 | */
52 | private long lastModifiedTime;
53 |
54 | /**
55 | * Version of the artifact.
56 | */
57 | private String version;
58 |
59 | /**
60 | * A key to uniquely identify an Artifact within a runtime.
61 | */
62 | private Object key;
63 |
64 | /**
65 | * Deployment directory that the artifact is associated with Eg : webapps, sequences, apis, etc.
66 | */
67 | private ArtifactType type;
68 |
69 | /**
70 | * To keep set of custom properties related to this artifact.
71 | */
72 | private Map properties = new HashMap<>();
73 |
74 | /**
75 | * Default constructor which takes the associated file with this artifact instance.
76 | *
77 | * @param file the associated file
78 | */
79 | public Artifact(File file) {
80 | this.file = file;
81 | }
82 |
83 | /**
84 | * Returns a meaningful name for this artifact.
85 | *
86 | * @return file name
87 | */
88 | public String getName() {
89 | return file.getName();
90 | }
91 |
92 | /**
93 | * A key is used to uniquely identify an Artifact within a runtime.
94 | *
95 | * @return key
96 | */
97 | public Object getKey() {
98 | return key;
99 | }
100 |
101 | /**
102 | * Sets a given key to this artifact instance.
103 | *
104 | * @param key key
105 | */
106 | public void setKey(Object key) {
107 | this.key = key;
108 | }
109 |
110 | /**
111 | * Path of the file associated with this artifact.
112 | *
113 | * @return path
114 | */
115 | public String getPath() {
116 | return file.getPath();
117 | }
118 |
119 | /**
120 | * The file associated with the artifact.
121 | *
122 | * @return file
123 | */
124 | public File getFile() {
125 | return file;
126 | }
127 |
128 | /**
129 | * Version of the artifact.
130 | *
131 | * @return version
132 | */
133 | public String getVersion() {
134 | return version;
135 | }
136 |
137 | /**
138 | * Sets a given version to this artifact instance.
139 | *
140 | * @param version version to be set
141 | */
142 | public void setVersion(String version) {
143 | this.version = version;
144 | }
145 |
146 | /**
147 | * ArtifactType of the artifact.
148 | * Eg : war, aar, dbs
149 | *
150 | * @return artifact directory
151 | */
152 | public ArtifactType getType() {
153 | return type;
154 | }
155 |
156 | /**
157 | * Sets the given directory for this artifact instance.
158 | *
159 | * @param type directory of the artifact to be set
160 | */
161 | public void setType(ArtifactType type) {
162 | this.type = type;
163 | }
164 |
165 | /**
166 | * This will return the last modified time of this artifact.
167 | *
168 | * @return last modified time
169 | */
170 | public long getLastModifiedTime() {
171 | return lastModifiedTime;
172 | }
173 |
174 | /**
175 | * Sets the last modified time of this artifact.
176 | *
177 | * @param lastModifiedTime lastModifiedTime
178 | */
179 | public void setLastModifiedTime(long lastModifiedTime) {
180 | this.lastModifiedTime = lastModifiedTime;
181 | }
182 |
183 | /**
184 | * This will return the Map of custom properties for this artifact.
185 | *
186 | * @return custom properties map
187 | */
188 | public Map getProperties() {
189 | return this.properties;
190 | }
191 |
192 | /**
193 | * This method allows to set custom properties which are needed for this Artifact.
194 | *
195 | * @param properties map of custom properties
196 | */
197 | public void setProperties(Map properties) {
198 | this.properties = properties;
199 | }
200 |
201 | }
202 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/main/java/org/wso2/carbon/deployment/engine/internal/CarbonDeploymentService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine.internal;
17 |
18 | import org.wso2.carbon.deployment.engine.Artifact;
19 | import org.wso2.carbon.deployment.engine.ArtifactType;
20 | import org.wso2.carbon.deployment.engine.Deployer;
21 | import org.wso2.carbon.deployment.engine.DeploymentService;
22 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
23 | import org.wso2.carbon.utils.FileUtils;
24 | import org.wso2.carbon.utils.Utils;
25 |
26 | import java.io.File;
27 | import java.io.IOException;
28 | import java.nio.file.Path;
29 | import java.nio.file.Paths;
30 |
31 | /**
32 | * This class represent the implementation of deployment, undeployment and redeployment of Carbon Artifacts.
33 | *
34 | * @since 5.0.0
35 | */
36 | public class CarbonDeploymentService implements DeploymentService {
37 |
38 | private DeploymentEngine carbonDeploymentEngine;
39 |
40 | /**
41 | * This will construct the CarbonDeploymentService using the given DeploymentEngine instance.
42 | *
43 | * @param carbonDeploymentEngine the DeploymentEngine instance used with constructing the CarbonDeploymentService
44 | */
45 | public CarbonDeploymentService(DeploymentEngine carbonDeploymentEngine) {
46 | this.carbonDeploymentEngine = carbonDeploymentEngine;
47 | }
48 |
49 | /**
50 | * This method will be called externally to deploy an artifact by giving the artifact deployment
51 | * directory and the path. The consumers of the DeploymentService will be calling this method.
52 | *
53 | * @param artifactPath path where the artifact resides. This has to be the full qualified path
54 | * of the artifact
55 | * @param artifactType the type of the artifact going to be dpeloyed
56 | * Eg : webapp, dataservice, sequence
57 | * @throws CarbonDeploymentException this will be thrown on error situation while trying deploy the given artifact
58 | */
59 | public void deploy(String artifactPath, ArtifactType artifactType)
60 | throws CarbonDeploymentException {
61 | Utils.checkSecurity();
62 | Deployer deployer = carbonDeploymentEngine.getDeployer(artifactType);
63 | if (deployer == null) {
64 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
65 | }
66 | try {
67 | Path destination = Paths.get(carbonDeploymentEngine.getServerRepositoryDirectory().getAbsolutePath(),
68 | deployer.getLocation().getFile());
69 | FileUtils.copyFileToDir(new File(artifactPath), destination.toFile());
70 | } catch (IOException e) {
71 | throw new CarbonDeploymentException("Error wile copying artifact", e);
72 | }
73 | }
74 |
75 | /**
76 | * This method will be called externally to undeploy an artifact by giving the artifact deployment
77 | * directory and the path. The consumers of the DeploymentService will be calling this method.
78 | *
79 | * @param key artifact key to uniquely identify an artifact in a runtime
80 | * Eg: for webapps this can be webapp context such as /foo , /bar, etc
81 | * for service this can be service name such as EchoService, VersionService, etc
82 | * @param artifactType the type of the artifact going to be deployed
83 | * Eg : webapp, dataservice, sequence
84 | * @throws CarbonDeploymentException this will be thrown on error situation while trying undeploy the given artifact
85 | */
86 | public void undeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException {
87 | Utils.checkSecurity();
88 | Deployer deployer = carbonDeploymentEngine.getDeployer(artifactType);
89 | if (deployer == null) {
90 | throw new CarbonDeploymentException("Unknown artifactType : " + artifactType);
91 | }
92 |
93 | Artifact deployedArtifact = carbonDeploymentEngine.getDeployedArtifact(artifactType, key);
94 | if (deployedArtifact != null) {
95 | FileUtils.deleteDir(new File(deployedArtifact.getPath()));
96 | } else {
97 | throw new CarbonDeploymentException("Cannot find artifact with key : " + key +
98 | " to undeploy");
99 | }
100 | }
101 |
102 | /**
103 | * This method is same as deploy and undeploy, will be called externally to undeploy an artifact by giving
104 | * the artifact deployment directory and the path. The consumers of the DeploymentService will be calling
105 | * this method.
106 | *
107 | * @param key artifact key to uniquely identify an artifact in a runtime
108 | * Eg: for webapps this can be webapp context such as /foo , /bar, etc
109 | * for service this can be service name such as EchoService, VersionService, etc
110 | * @param artifactType the type of the artifact going to be deployed
111 | * Eg : webapp, dataservice, sequence
112 | * @throws CarbonDeploymentException throws redeployment exceptions
113 | */
114 | public void redeploy(Object key, ArtifactType artifactType) throws CarbonDeploymentException {
115 | // TODO implement this
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/src/test/java/org/wso2/carbon/deployment/engine/deployers/CustomDeployer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine.deployers;
17 |
18 | import org.slf4j.Logger;
19 | import org.slf4j.LoggerFactory;
20 | import org.wso2.carbon.deployment.engine.Artifact;
21 | import org.wso2.carbon.deployment.engine.ArtifactType;
22 | import org.wso2.carbon.deployment.engine.Deployer;
23 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
24 |
25 | import java.io.File;
26 | import java.io.FileInputStream;
27 | import java.io.IOException;
28 | import java.net.MalformedURLException;
29 | import java.net.URL;
30 |
31 | /**
32 | * Custom Deployer class to test deployment.
33 | *
34 | * @since 5.0.0
35 | */
36 | public class CustomDeployer implements Deployer {
37 | private static final Logger logger = LoggerFactory.getLogger(CustomDeployer.class);
38 | /**
39 | * Has init() been called?.
40 | */
41 | public static boolean initCalled;
42 | /**
43 | * Set to true if "XML1" has been deployed.
44 | */
45 | public static boolean sample1Deployed;
46 | /**
47 | * Set to true if "XML2" has been deployed.
48 | */
49 | public static boolean sample2Deployed;
50 | /**
51 | * Set to true if "XML1" has been updated.
52 | */
53 | public static boolean sample1Updated;
54 | /**
55 | * Set to true if "XML2" has been updated.
56 | */
57 | public static boolean sample2Updated;
58 |
59 | private String directory = "text-files";
60 | private URL directoryLocation;
61 | private ArtifactType artifactType;
62 | private String testDir = "src" + File.separator + "test" + File.separator + "resources" +
63 | File.separator + "carbon-repo" + File.separator + directory;
64 | private String testDir2 = "src" + File.separator + "test" + File.separator + "resources" +
65 | File.separator + "deployment" + File.separator + directory;
66 |
67 | public CustomDeployer() {
68 | artifactType = new ArtifactType("txt");
69 | try {
70 | directoryLocation = new URL("file:text-files");
71 | } catch (MalformedURLException e) {
72 | logger.error("Error while initializing directoryLocation", e);
73 | }
74 | }
75 |
76 | public void init() {
77 | logger.info("Initializing Deployer");
78 | initCalled = true;
79 | }
80 |
81 | public String deploy(Artifact artifact) throws CarbonDeploymentException {
82 | logger.info("Deploying : " + artifact.getName());
83 | String key = null;
84 | try (FileInputStream fis = new FileInputStream(artifact.getFile())) {
85 | int x = fis.available();
86 | byte b[] = new byte[x];
87 | fis.read(b);
88 | String content = new String(b);
89 | if (content.contains("sample1")) {
90 | sample1Deployed = true;
91 | key = artifact.getName();
92 | } else if (content.contains("sample2")) {
93 | sample2Deployed = true;
94 | key = artifact.getName();
95 | }
96 | } catch (IOException e) {
97 | throw new CarbonDeploymentException("Error while deploying : " + artifact.getName(), e);
98 | }
99 | return key;
100 | }
101 |
102 | public void undeploy(Object key) throws CarbonDeploymentException {
103 | if (!(key instanceof String)) {
104 | throw new CarbonDeploymentException("Error while Un Deploying : " + key +
105 | "is not a String value");
106 | }
107 | logger.info("Undeploying : " + key);
108 | File fileToUndeploy;
109 | if ("sample1.txt".equals(key)) {
110 | fileToUndeploy = new File(testDir + File.separator + key);
111 | } else if ("sample2.txt".equals(key)) {
112 | fileToUndeploy = new File(testDir2 + File.separator + key);
113 | } else {
114 | throw new CarbonDeploymentException("Error while Un Deploying : " + key);
115 | }
116 | logger.info("File to undeploy : " + fileToUndeploy.getAbsolutePath());
117 | try (FileInputStream fis = new FileInputStream(fileToUndeploy)) {
118 | int x = fis.available();
119 | byte b[] = new byte[x];
120 | fis.read(b);
121 | String content = new String(b);
122 | if (content.contains("sample1")) {
123 | sample1Deployed = false;
124 | } else if (content.contains("sample2")) {
125 | sample2Deployed = false;
126 | }
127 | } catch (IOException e) {
128 | throw new CarbonDeploymentException("Error while Un Deploying : " + key, e);
129 | }
130 | }
131 |
132 | public String update(Artifact artifact) throws CarbonDeploymentException {
133 | logger.info("Updating : " + artifact.getName());
134 | try (FileInputStream fis = new FileInputStream(artifact.getFile())) {
135 | int x = fis.available();
136 | byte b[] = new byte[x];
137 | fis.read(b);
138 | String content = new String(b);
139 | if (content.contains("sample1")) {
140 | sample1Updated = true;
141 | } else if (content.contains("sample2")) {
142 | sample2Updated = true;
143 | }
144 | } catch (IOException e) {
145 | throw new CarbonDeploymentException("Error while updating : " + artifact.getName(), e);
146 | }
147 | return artifact.getName();
148 | }
149 |
150 |
151 | public URL getLocation() {
152 | return directoryLocation;
153 | }
154 |
155 | public void setLocation(URL directoryLocation) {
156 | this.directoryLocation = directoryLocation;
157 | }
158 |
159 | public ArtifactType getArtifactType() {
160 | return artifactType;
161 | }
162 |
163 | public void setArtifactType(ArtifactType artifactType) {
164 | this.artifactType = artifactType;
165 | }
166 | }
167 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.notifier/src/test/java/org/wso2/carbon/deployment/notifier/deployers/CustomDeployer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.notifier.deployers;
17 |
18 | import org.slf4j.Logger;
19 | import org.slf4j.LoggerFactory;
20 | import org.wso2.carbon.deployment.engine.Artifact;
21 | import org.wso2.carbon.deployment.engine.ArtifactType;
22 | import org.wso2.carbon.deployment.engine.Deployer;
23 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
24 |
25 | import java.io.File;
26 | import java.io.FileInputStream;
27 | import java.io.IOException;
28 | import java.net.MalformedURLException;
29 | import java.net.URL;
30 |
31 | /**
32 | * Custom Deployer class to test deployment.
33 | *
34 | * @since 5.0.0
35 | */
36 | public class CustomDeployer implements Deployer {
37 | private static final Logger logger = LoggerFactory.getLogger(CustomDeployer.class);
38 | /**
39 | * Has init() been called?.
40 | */
41 | public static boolean initCalled;
42 | /**
43 | * Set to true if "XML1" has been deployed.
44 | */
45 | public static boolean sample1Deployed;
46 | /**
47 | * Set to true if "XML2" has been deployed.
48 | */
49 | public static boolean sample2Deployed;
50 | /**
51 | * Set to true if "XML1" has been updated.
52 | */
53 | public static boolean sample1Updated;
54 | /**
55 | * Set to true if "XML2" has been updated.
56 | */
57 | public static boolean sample2Updated;
58 |
59 | private String directory = "text-files";
60 | private URL directoryLocation;
61 | private ArtifactType artifactType;
62 | private String testDir = "src" + File.separator + "test" + File.separator + "resources" +
63 | File.separator + "carbon-repo" + File.separator + directory;
64 | private String testDir2 = "src" + File.separator + "test" + File.separator + "resources" +
65 | File.separator + "deployment" + File.separator + directory;
66 |
67 | public CustomDeployer() {
68 | artifactType = new ArtifactType("txt");
69 | try {
70 | directoryLocation = new URL("file:text-files");
71 | } catch (MalformedURLException e) {
72 | logger.error("Error while initializing directoryLocation", e);
73 | }
74 | }
75 |
76 | public void init() {
77 | logger.info("Initializing Deployer");
78 | initCalled = true;
79 | }
80 |
81 | public String deploy(Artifact artifact) throws CarbonDeploymentException {
82 | logger.info("Deploying : " + artifact.getName());
83 | String key = null;
84 | try {
85 | FileInputStream fis = new FileInputStream(artifact.getFile());
86 | int x = fis.available();
87 | byte b[] = new byte[x];
88 | fis.read(b);
89 | String content = new String(b);
90 | if (content.contains("sample1")) {
91 | sample1Deployed = true;
92 | key = artifact.getName();
93 | } else if (content.contains("sample2")) {
94 | sample2Deployed = true;
95 | key = artifact.getName();
96 | }
97 | } catch (IOException e) {
98 | throw new CarbonDeploymentException("Error while deploying : " + artifact.getName(), e);
99 | }
100 | return key;
101 | }
102 |
103 | public void undeploy(Object key) throws CarbonDeploymentException {
104 | if (!(key instanceof String)) {
105 | throw new CarbonDeploymentException("Error while Un Deploying : " + key +
106 | "is not a String value");
107 | }
108 |
109 | logger.info("Undeploying : " + key);
110 | File fileToUndeploy;
111 | if ("sample1.txt".equals(key)) {
112 | fileToUndeploy = new File(testDir + File.separator + key);
113 | } else if ("sample2.txt".equals(key)) {
114 | fileToUndeploy = new File(testDir2 + File.separator + key);
115 | } else {
116 | throw new CarbonDeploymentException("Error while Un Deploying : " + key);
117 | }
118 | logger.info("File to undeploy : " + fileToUndeploy.getAbsolutePath());
119 | try (FileInputStream fis = new FileInputStream(fileToUndeploy)) {
120 | int x = fis.available();
121 | byte b[] = new byte[x];
122 | fis.read(b);
123 | String content = new String(b);
124 | if (content.contains("sample1")) {
125 | sample1Deployed = false;
126 | } else if (content.contains("sample2")) {
127 | sample2Deployed = false;
128 | }
129 | } catch (IOException e) {
130 | throw new CarbonDeploymentException("Error while Un Deploying : " + key, e);
131 | }
132 | }
133 |
134 | public String update(Artifact artifact) throws CarbonDeploymentException {
135 | logger.info("Updating : " + artifact.getName());
136 | try (FileInputStream fis = new FileInputStream(artifact.getFile())) {
137 | int x = fis.available();
138 | byte b[] = new byte[x];
139 | fis.read(b);
140 | String content = new String(b);
141 | if (content.contains("sample1")) {
142 | sample1Updated = true;
143 | } else if (content.contains("sample2")) {
144 | sample2Updated = true;
145 | }
146 | } catch (IOException e) {
147 | throw new CarbonDeploymentException("Error while updating : " + artifact.getName(), e);
148 | }
149 | return artifact.getName();
150 | }
151 |
152 |
153 | public URL getLocation() {
154 | return directoryLocation;
155 | }
156 |
157 | public void setLocation(URL directoryLocation) {
158 | this.directoryLocation = directoryLocation;
159 | }
160 |
161 | public ArtifactType getArtifactType() {
162 | return artifactType;
163 | }
164 |
165 | public void setArtifactType(ArtifactType artifactType) {
166 | this.artifactType = artifactType;
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/components/org.wso2.carbon.deployment.engine/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
21 | org.wso2.carbon.deployment
22 | org.wso2.carbon.deployment.parent
23 | 5.1.8-SNAPSHOT
24 | ../../pom.xml
25 |
26 |
27 | 4.0.0
28 | org.wso2.carbon.deployment.engine
29 | 5.1.8-SNAPSHOT
30 | bundle
31 | WSO2 Carbon Deployment Engine
32 |
33 | The core bundle which includes the Deployment Framework
34 |
35 | http://wso2.com
36 |
37 |
38 |
39 | org.wso2.eclipse.osgi
40 | org.eclipse.osgi
41 | true
42 |
43 |
44 | org.wso2.eclipse.osgi
45 | org.eclipse.osgi.services
46 | true
47 |
48 |
49 | org.ops4j.pax.logging
50 | pax-logging-api
51 | true
52 |
53 |
54 | org.yaml
55 | snakeyaml
56 | true
57 |
58 |
59 | org.testng
60 | testng
61 | test
62 |
63 |
64 | org.jacoco
65 | org.jacoco.agent
66 | runtime
67 | test
68 |
69 |
70 | commons-io.wso2
71 | commons-io
72 | test
73 |
74 |
75 | org.wso2.carbon
76 | org.wso2.carbon.core
77 |
78 |
79 | org.wso2.carbon.utils
80 | org.wso2.carbon.utils
81 |
82 |
83 | org.wso2.carbon.config
84 | org.wso2.carbon.config
85 |
86 |
87 | org.easymock
88 | easymock
89 | test
90 |
91 |
92 |
93 |
94 |
95 |
96 | maven-surefire-plugin
97 | false
98 |
99 |
100 | src/test/resources/testng.xml
101 |
102 |
103 |
104 |
105 | org.apache.maven.plugins
106 | maven-source-plugin
107 |
108 |
109 | org.jacoco
110 | jacoco-maven-plugin
111 |
112 |
113 | org.apache.maven.plugins
114 | maven-javadoc-plugin
115 |
116 |
117 | attach-javadocs
118 |
119 | jar
120 |
121 |
122 | org.wso2.carbon.deployment.engine.internal
123 | -Xdoclint:none
124 | public
125 | true
126 |
127 |
128 |
129 |
130 |
131 | org.wso2.carbon.config
132 | org.wso2.carbon.config.maven.plugin
133 |
134 |
135 |
136 | create-doc
137 |
138 | compile
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 | org.wso2.carbon.deployment.engine.internal.*,
147 |
148 | !org.wso2.carbon.deployment.engine.internal.*,
149 | org.wso2.carbon.deployment.engine.*;version="${carbon.deployment.pkg.export.version}",
150 |
151 |
152 | org.slf4j.*;version="${slf4j.logging.import.version.range}",
153 | org.osgi.framework.*;version="${osgi.framework.import.version.range}",
154 | org.yaml.snakeyaml.*;version="${org.snakeyaml.import.version.range}",
155 | org.wso2.carbon.kernel.*;version="${carbon.kernel.import.version.range}",
156 | org.wso2.carbon.utils.*;version="${carbon.utils.package.import.version.range}",
157 | org.wso2.carbon.config.*;version="${carbon.config.package.import.version.range}"
158 |
159 |
160 | startup.listener;componentName="carbon-deployment-service";
161 | requiredService="org.wso2.carbon.deployment.engine.Deployer,
162 | org.wso2.carbon.deployment.engine.LifecycleListener",
163 |
164 | osgi.service;objectClass="org.wso2.carbon.deployment.engine.DeploymentService";
165 | dependentComponentName="carbon-transport-mgt"
166 |
167 |
168 |
169 |
170 |
--------------------------------------------------------------------------------
/tests/osgi-tests/src/test/java/org/wso2/carbon/deployment/engine/osgi/CarbonDeploymentEngineOSGiTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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 | package org.wso2.carbon.deployment.engine.osgi;
17 |
18 | import org.ops4j.pax.exam.ExamFactory;
19 | import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
20 | import org.ops4j.pax.exam.spi.reactors.PerClass;
21 | import org.ops4j.pax.exam.testng.listener.PaxExam;
22 | import org.osgi.framework.BundleContext;
23 | import org.osgi.framework.InvalidSyntaxException;
24 | import org.osgi.framework.ServiceReference;
25 | import org.osgi.framework.ServiceRegistration;
26 | import org.testng.Assert;
27 | import org.testng.annotations.Listeners;
28 | import org.testng.annotations.Test;
29 | import org.wso2.carbon.container.CarbonContainerFactory;
30 | import org.wso2.carbon.deployment.engine.ArtifactType;
31 | import org.wso2.carbon.deployment.engine.Deployer;
32 | import org.wso2.carbon.deployment.engine.DeploymentService;
33 | import org.wso2.carbon.deployment.engine.LifecycleListener;
34 | import org.wso2.carbon.deployment.engine.exception.CarbonDeploymentException;
35 |
36 | import java.nio.file.Path;
37 | import java.nio.file.Paths;
38 | import java.util.Optional;
39 | import java.util.stream.Stream;
40 | import javax.inject.Inject;
41 |
42 | /**
43 | * Carbon Deployment Engine OSGi Test case.
44 | *
45 | * @since 5.0.0
46 | */
47 | @Listeners(PaxExam.class)
48 | @ExamReactorStrategy(PerClass.class)
49 | @ExamFactory(CarbonContainerFactory.class)
50 | public class CarbonDeploymentEngineOSGiTest {
51 |
52 | public static final String DEPLOYMENT_YAML = "deployment.yaml";
53 |
54 | @Inject
55 | private BundleContext bundleContext;
56 |
57 | @Inject
58 | private DeploymentService deploymentService;
59 |
60 | private static String artifactPath;
61 |
62 | static {
63 | String basedir = System.getProperty("basedir");
64 | if (basedir == null) {
65 | basedir = Paths.get("..", "..", "..", "..").toString();
66 | }
67 | Path testResourceDir = Paths.get(basedir, "src", "test", "resources");
68 | artifactPath = Paths.get(testResourceDir.toString(), "deployment", "text-files", "sample1.txt").toString();
69 | }
70 |
71 | @Test
72 | public void testRegisterDeployer() {
73 | ServiceRegistration serviceRegistration = bundleContext.registerService(Deployer.class.getName(),
74 | new CustomDeployer(), null);
75 | ServiceReference reference = bundleContext.getServiceReference(Deployer.class.getName());
76 | Assert.assertNotNull(reference, "Custom Deployer Service Reference is null");
77 | CustomDeployer deployer = (CustomDeployer) bundleContext.getService(reference);
78 | Assert.assertNotNull(deployer, "Custom Deployer Service is null");
79 | serviceRegistration.unregister();
80 | reference = bundleContext.getServiceReference(Deployer.class.getName());
81 | Assert.assertNull(reference, "Custom Deployer Service Reference should be unregistered and null");
82 |
83 | //register faulty deployers
84 | CustomDeployer deployer1 = new CustomDeployer();
85 | deployer1.setArtifactType(null);
86 | bundleContext.registerService(Deployer.class.getName(), deployer1, null);
87 |
88 | CustomDeployer deployer2 = new CustomDeployer();
89 | deployer2.setLocation(null);
90 | bundleContext.registerService(Deployer.class.getName(), deployer2, null);
91 | }
92 |
93 | @Test
94 | public void testRegisterLifecycleListener() throws InvalidSyntaxException {
95 | ServiceRegistration serviceRegistration = bundleContext.registerService(LifecycleListener.class.getName(),
96 | new CustomLifecycleListener(), null);
97 | ServiceReference[] references = bundleContext.getServiceReferences(LifecycleListener.class.getName(), null);
98 | Assert.assertNotNull(references, "The CustomLifecycleListener service reference cannot be found.");
99 |
100 | Optional reference = Stream.of(references).filter(this::isCustomLC).findFirst();
101 |
102 |
103 | Assert.assertTrue(reference.isPresent(), "The CustomLifecycleListener is not registered.");
104 |
105 | LifecycleListener listener = (LifecycleListener) bundleContext.getService(reference.get());
106 | Assert.assertNotNull(listener, "The CustomLifecycleListener is not registered.");
107 |
108 | serviceRegistration.unregister();
109 | references = bundleContext.getServiceReferences(LifecycleListener.class.getName(), null);
110 | reference = Stream.of(references).filter(this::isCustomLC).findFirst();
111 | Assert.assertFalse(reference.isPresent(), "The CustomLifecycleListener service un-registration failed.");
112 | }
113 |
114 | private boolean isCustomLC(ServiceReference serviceReference) {
115 | String listenerClass = bundleContext.getService(serviceReference).getClass().getName();
116 | return listenerClass.equals("org.wso2.carbon.deployment.engine.osgi.CustomLifecycleListener");
117 | }
118 |
119 | @Test(dependsOnMethods = {"testRegisterDeployer"})
120 | public void testDeploymentService() throws CarbonDeploymentException {
121 | Assert.assertNotNull(deploymentService);
122 | CustomDeployer customDeployer = new CustomDeployer();
123 | bundleContext.registerService(Deployer.class.getName(), customDeployer, null);
124 | bundleContext.registerService(LifecycleListener.class.getName(), new CustomLifecycleListener(), null);
125 |
126 | //undeploy
127 | try {
128 | deploymentService.undeploy(artifactPath, new ArtifactType<>("unknown"));
129 | } catch (CarbonDeploymentException e) {
130 | Assert.assertTrue(e.getMessage().contains("Unknown artifactType"));
131 | }
132 | try {
133 | deploymentService.undeploy("fake.path", customDeployer.getArtifactType());
134 | } catch (CarbonDeploymentException e) {
135 | Assert.assertEquals(e.getMessage(), "Cannot find artifact with key : fake.path to undeploy");
136 | }
137 | try {
138 | deploymentService.undeploy(artifactPath, customDeployer.getArtifactType());
139 | } catch (CarbonDeploymentException e) {
140 | Assert.assertEquals(e.getMessage(), "Cannot find artifact with key : " + artifactPath + " to undeploy");
141 | }
142 | //deploy
143 | try {
144 | deploymentService.deploy("fake.path", customDeployer.getArtifactType());
145 | } catch (CarbonDeploymentException e) {
146 | Assert.assertTrue(e.getMessage().contains("Error wile copying artifact"));
147 | }
148 | try {
149 | deploymentService.deploy(artifactPath, new ArtifactType<>("unknown"));
150 | } catch (CarbonDeploymentException e) {
151 | Assert.assertTrue(e.getMessage().contains("Unknown artifactType"));
152 | }
153 | deploymentService.deploy(artifactPath, customDeployer.getArtifactType());
154 |
155 | //redeploy - this does not do anything for the moment.
156 | deploymentService.redeploy(artifactPath, customDeployer.getArtifactType());
157 | }
158 |
159 | }
160 |
--------------------------------------------------------------------------------