提交 104dc002 编写于 作者: E Elias Ricken de Medeiros

Import processes from application zip file

上级 9fa8deee
......@@ -33,6 +33,11 @@
<artifactId>activiti-connector-model</artifactId>
<version>${activiti-core-common.version}</version>
</dependency>
<dependency>
<groupId>org.activiti.core.common</groupId>
<artifactId>activiti-spring-application</artifactId>
<version>${activiti-core-common.version}</version>
</dependency>
<dependency>
<groupId>org.activiti.core.common</groupId>
<artifactId>activiti-spring-connector</artifactId>
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018 Alfresco, Inc. and/or its affiliates.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.activiti.core.common</groupId>
<artifactId>activiti-core-common-parent</artifactId>
<version>7.0.0-SNAPSHOT</version>
</parent>
<artifactId>activiti-spring-application</artifactId>
<name>Activiti Core Common :: Application</name>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ApplicationContent {
private Map<String, List<FileContent>> entries = new HashMap<>();
public void add(ApplicationEntry entry) {
List<FileContent> fileContents = entries.computeIfAbsent(entry.getType(),
k -> new ArrayList<>());
fileContents.add(entry.getFileContent());
}
public List<FileContent> getFileContents(String entryType) {
return entries.getOrDefault(entryType, Collections.emptyList());
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
public class ApplicationDiscovery {
private static final String APPLICATIONS_LOCATION = "classpath:/applications/";
private ResourcePatternResolver resourceLoader;
public ApplicationDiscovery(ResourcePatternResolver resourceLoader) {
this.resourceLoader = resourceLoader;
}
public List<Resource> discoverApplications() {
List<Resource> resources = new ArrayList<>();
Resource resource = resourceLoader.getResource(APPLICATIONS_LOCATION);
if (resource.exists()) {
try {
resources = Arrays.asList(resourceLoader.getResources(APPLICATIONS_LOCATION + "**.zip"));
} catch (IOException e) {
//fixme
e.printStackTrace();
}
}
return resources;
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
public class ApplicationEntry {
private String type;
private FileContent fileContent;
public ApplicationEntry(String type,
FileContent fileContent) {
this.type = type;
this.fileContent = fileContent;
}
public String getType() {
return type;
}
public FileContent getFileContent() {
return fileContent;
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
import java.util.function.Predicate;
import java.util.zip.ZipEntry;
public interface ApplicationEntryDiscovery {
Predicate<ZipEntry> filter(ZipEntry entry);
String getEntryType();
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.io.Resource;
public class ApplicationLoader {
private ApplicationDiscovery applicationDiscovery;
private ApplicationReader applicationReader;
public ApplicationLoader(ApplicationDiscovery applicationDiscovery,
ApplicationReader applicationReader) {
this.applicationDiscovery = applicationDiscovery;
this.applicationReader = applicationReader;
}
public List<ApplicationContent> loadApplications(){
List<ApplicationContent> applications = new ArrayList<>();
List<Resource> applicationResources = applicationDiscovery.discoverApplications();
for (Resource applicationResource : applicationResources) {
try (InputStream inputStream = applicationResource.getInputStream()) {
applications.add(applicationReader.read(inputStream));
} catch (IOException e) {
//fixme
e.printStackTrace();
}
}
return applications;
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;
public class ApplicationReader {
private List<ApplicationEntryDiscovery> applicationEntryDiscoveries;
public ApplicationReader(List<ApplicationEntryDiscovery> applicationEntryDiscoveries) {
this.applicationEntryDiscoveries = applicationEntryDiscoveries;
}
public ApplicationContent read(InputStream inputStream) {
ApplicationContent application = new ApplicationContent();
try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
ZipEntry currentEntry = zipEntry;
applicationEntryDiscoveries
.stream()
.filter(applicationEntryDiscovery -> applicationEntryDiscovery.filter(currentEntry).test(currentEntry))
.findFirst()
.ifPresent(
applicationEntryDiscovery ->
application.add(new ApplicationEntry(applicationEntryDiscovery.getEntryType(),
new FileContent(currentEntry.getName(),
readBytes(zipInputStream
)))));
}
} catch (IOException e) {
//fixme
e.printStackTrace();
}
return application;
}
private byte[] readBytes(ZipInputStream zipInputStream) {
try {
return IOUtils.toByteArray(zipInputStream);
} catch (IOException e) {
//fixme
e.printStackTrace();
}
return null;
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
public class FileContent {
private String name;
private byte [] content;
public FileContent(String name,
byte[] content) {
this.name = name;
this.content = content;
}
public String getName() {
return name;
}
public byte[] getContent() {
return content;
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application.conf;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.activiti.application.ApplicationDiscovery;
import org.activiti.application.ApplicationEntryDiscovery;
import org.activiti.application.ApplicationLoader;
import org.activiti.application.ApplicationReader;
import org.activiti.application.deployer.ApplicationDeployer;
import org.activiti.application.deployer.ApplicationEntryDeployer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.ResourcePatternResolver;
@Configuration
public class ApplicationAutoConfiguration {
@Bean
public InitializingBean deployApplications(ResourcePatternResolver resourceLoader,
@Autowired(required = false) List<ApplicationEntryDiscovery> applicationEntryDiscoveries,
@Autowired(required = false) List<ApplicationEntryDeployer> applicationEntryDeployers) {
return () -> new ApplicationDeployer(new ApplicationLoader(new ApplicationDiscovery(resourceLoader),
new ApplicationReader(
Optional.ofNullable(applicationEntryDiscoveries)
.orElse(Collections.emptyList()))),
Optional.ofNullable(applicationEntryDeployers)
.orElse(Collections.emptyList())).deploy();
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application.deployer;
import java.util.List;
import org.activiti.application.ApplicationContent;
import org.activiti.application.ApplicationLoader;
public class ApplicationDeployer {
private ApplicationLoader applicationLoader;
private List<ApplicationEntryDeployer> deployers;
public ApplicationDeployer(ApplicationLoader applicationLoader,
List<ApplicationEntryDeployer> deployers) {
this.applicationLoader = applicationLoader;
this.deployers = deployers;
}
public void deploy() {
List<ApplicationContent> applications = applicationLoader.loadApplications();
for (ApplicationContent application : applications) {
for (ApplicationEntryDeployer deployer : deployers) {
deployer.deployEntries(application);
}
}
}
}
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application.deployer;
import org.activiti.application.ApplicationContent;
public interface ApplicationEntryDeployer {
void deployEntries(ApplicationContent application);
}
# Activiti auto-configurations
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.activiti.application.conf.ApplicationAutoConfiguration
\ No newline at end of file
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.MockitoAnnotations.initMocks;
public class ApplicationLoaderTest {
@InjectMocks
private ApplicationLoader loader;
@Mock
private ApplicationDiscovery applicationDiscovery;
@Mock
private ApplicationReader applicationReader;
@Before
public void setUp() {
initMocks(this);
}
@Test
public void shouldLoadApplications() throws Exception {
//given
Resource applicationResource = mock(Resource.class);
given(applicationResource.getInputStream()).willReturn(mock(InputStream.class));
given(applicationDiscovery.discoverApplications()).willReturn(Collections.singletonList(applicationResource));
ApplicationContent applicationContent = new ApplicationContent();
given(applicationReader.read(applicationResource.getInputStream())).willReturn(applicationContent);
//when
List<ApplicationContent> applicationContents = loader.loadApplications();
//then
assertThat(applicationContents).containsExactly(applicationContent);
}
}
\ No newline at end of file
/*
* Copyright 2018 Alfresco, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.application.deployer;
import java.util.Arrays;
import org.activiti.application.ApplicationContent;
import org.activiti.application.ApplicationLoader;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;
public class ApplicationDeployerTest {
private ApplicationDeployer deployer;
@Mock
private ApplicationLoader applicationLoader;
@Mock
private ApplicationEntryDeployer firstDeployer;
@Mock
private ApplicationEntryDeployer secondDeployer;
@Before
public void setUp() {
initMocks(this);
deployer = new ApplicationDeployer(applicationLoader,
Arrays.asList(firstDeployer, secondDeployer));
}
@Test
public void shouldDelegateDeployToEntryDeployers() {
//given
ApplicationContent firstApp = mock(ApplicationContent.class);
ApplicationContent secondApp = mock(ApplicationContent.class);
given(applicationLoader.loadApplications()).willReturn(Arrays.asList(firstApp,
secondApp));
//when
deployer.deploy();
//then
verify(firstDeployer).deployEntries(firstApp);
verify(firstDeployer).deployEntries(secondApp);
verify(secondDeployer).deployEntries(firstApp);
verify(secondDeployer).deployEntries(secondApp);
}
}
\ No newline at end of file
......@@ -19,6 +19,7 @@
<activiti-build.version>7.0.39</activiti-build.version>
<activiti-api.version>7.0.34</activiti-api.version>
<activiti-core-common.version>${project.version}</activiti-core-common.version>
<commons-io.version>2.6</commons-io.version>
</properties>
<dependencyManagement>
<dependencies>
......@@ -36,6 +37,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<!-- Activiti Modules for managed dependencies -->
<!-- This duplicates activiti-core-common-dependencies module but we can't import that module here as this is its parent-->
<dependency>
......@@ -73,6 +79,7 @@
<module>activiti-spring-connector</module>
<module>activiti-connector-model</module>
<module>activiti-core-common-dependencies</module>
<module>activiti-spring-application</module>
</modules>
<repositories>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册