From cb89abd4699497a890ca5881ba5ebaedec94e083 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 1 Jan 2014 02:19:21 +0100 Subject: [PATCH] [New] Added new services AddResource*Service, AddOrder*Service, ImportModelFromXmlService --- .../li/strolch/service/AbstractService.java | 30 +++++-- .../service/DefaultServiceHandler.java | 25 +++++- src/main/java/li/strolch/service/Service.java | 3 - .../li/strolch/service/ServiceArgument.java | 17 +++- .../li/strolch/service/ServiceHandler.java | 2 +- .../li/strolch/service/ServiceResult.java | 8 +- .../model/AddOrderCollectionService.java | 57 +++++++++++++ .../service/model/AddOrderService.java | 52 ++++++++++++ .../model/AddResourceCollectionService.java | 57 +++++++++++++ .../service/model/AddResourceService.java | 52 ++++++++++++ .../model/ImportModelFromXmlService.java | 81 +++++++++++++++++++ 11 files changed, 368 insertions(+), 16 deletions(-) create mode 100644 src/main/java/li/strolch/service/model/AddOrderCollectionService.java create mode 100644 src/main/java/li/strolch/service/model/AddOrderService.java create mode 100644 src/main/java/li/strolch/service/model/AddResourceCollectionService.java create mode 100644 src/main/java/li/strolch/service/model/AddResourceService.java create mode 100644 src/main/java/li/strolch/service/model/ImportModelFromXmlService.java diff --git a/src/main/java/li/strolch/service/AbstractService.java b/src/main/java/li/strolch/service/AbstractService.java index 9682676ca..7fe175264 100644 --- a/src/main/java/li/strolch/service/AbstractService.java +++ b/src/main/java/li/strolch/service/AbstractService.java @@ -18,7 +18,9 @@ package li.strolch.service; import java.text.MessageFormat; import li.strolch.exception.StrolchException; -import li.strolch.runtime.agent.api.ComponentContainer; +import li.strolch.runtime.agent.api.OrderMap; +import li.strolch.runtime.agent.api.ResourceMap; +import li.strolch.runtime.configuration.RuntimeConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,18 +33,30 @@ public abstract class AbstractService V getComponent(Class clazz) { + return this.serviceHandler.getComponent(clazz); + } + + public RuntimeConfiguration getRuntimeConfiguration() { + return this.serviceHandler.getRuntimeConfiguration(); + } + + public ResourceMap getResourceMap(String realm) { + return this.serviceHandler.getResourceMap(realm); + } + + public OrderMap getOrderMap(String realm) { + return this.serviceHandler.getOrderMap(realm); } @Override diff --git a/src/main/java/li/strolch/service/DefaultServiceHandler.java b/src/main/java/li/strolch/service/DefaultServiceHandler.java index 3dbe77c5c..9e0963dc9 100644 --- a/src/main/java/li/strolch/service/DefaultServiceHandler.java +++ b/src/main/java/li/strolch/service/DefaultServiceHandler.java @@ -18,9 +18,12 @@ package li.strolch.service; import java.text.MessageFormat; import li.strolch.exception.StrolchException; +import li.strolch.runtime.agent.api.OrderMap; +import li.strolch.runtime.agent.api.ResourceMap; import li.strolch.runtime.agent.api.StrolchComponent; import li.strolch.runtime.agent.impl.ComponentContainerImpl; import li.strolch.runtime.configuration.ComponentConfiguration; +import li.strolch.runtime.configuration.RuntimeConfiguration; import li.strolch.runtime.privilege.StrolchPrivilegeHandler; import ch.eitchnet.privilege.model.Certificate; import ch.eitchnet.privilege.model.PrivilegeContext; @@ -28,10 +31,10 @@ import ch.eitchnet.utils.helper.StringHelper; /** * @author Robert von Burg - * */ public class DefaultServiceHandler extends StrolchComponent implements ServiceHandler { + private RuntimeConfiguration runtimeConfiguration; private StrolchPrivilegeHandler privilegeHandler; /** @@ -46,9 +49,26 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa public void initialize(ComponentConfiguration configuration) { if (getContainer().hasComponent(StrolchPrivilegeHandler.class)) this.privilegeHandler = getContainer().getComponent(StrolchPrivilegeHandler.class); + this.runtimeConfiguration = configuration.getRuntimeConfiguration(); super.initialize(configuration); } + public T getComponent(Class clazz) { + return getContainer().getComponent(clazz); + } + + public RuntimeConfiguration getRuntimeConfiguration() { + return this.runtimeConfiguration; + } + + public ResourceMap getResourceMap(String realm) { + return this.getContainer().getResourceMap(realm); + } + + public OrderMap getOrderMap(String realm) { + return this.getContainer().getOrderMap(realm); + } + @Override public U doService(Certificate certificate, Service service) { return doService(certificate, service, null); @@ -74,7 +94,8 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa try { // then perform the service - service.setContainer(getContainer()); + if (service instanceof AbstractService) + ((AbstractService) service).setServiceHandler(this); U serviceResult = service.doService(argument); if (serviceResult == null) { String msg = "Service {0} is not properly implemented as it returned a null result!"; //$NON-NLS-1$ diff --git a/src/main/java/li/strolch/service/Service.java b/src/main/java/li/strolch/service/Service.java index 03c76703e..de558710b 100644 --- a/src/main/java/li/strolch/service/Service.java +++ b/src/main/java/li/strolch/service/Service.java @@ -17,7 +17,6 @@ package li.strolch.service; import java.io.Serializable; -import li.strolch.runtime.agent.api.ComponentContainer; import ch.eitchnet.privilege.model.Restrictable; /** @@ -26,6 +25,4 @@ import ch.eitchnet.privilege.model.Restrictable; public interface Service extends Serializable, Restrictable { public U doService(T argument); - - public void setContainer(ComponentContainer container); } diff --git a/src/main/java/li/strolch/service/ServiceArgument.java b/src/main/java/li/strolch/service/ServiceArgument.java index 560717086..c81eb5622 100644 --- a/src/main/java/li/strolch/service/ServiceArgument.java +++ b/src/main/java/li/strolch/service/ServiceArgument.java @@ -17,11 +17,26 @@ package li.strolch.service; import java.io.Serializable; +import li.strolch.runtime.StrolchConstants; + /** + * Base argument to be used when performing {@link Service Services}. The realm parameter is set to + * {@link StrolchConstants#DEFAULT_REALM} and can be overridden when the caller of the service wants to perform the + * service in a different realm + * * @author Robert von Burg */ public class ServiceArgument implements Serializable { private static final long serialVersionUID = 1L; - // marker interface + /** + *

+ * Set this to the realm in which the service should operate + *

+ * + *

+ * realm = StrolchConstants.DEFAULT_REALM + *

+ */ + public String realm = StrolchConstants.DEFAULT_REALM; } diff --git a/src/main/java/li/strolch/service/ServiceHandler.java b/src/main/java/li/strolch/service/ServiceHandler.java index 334ca95c0..32ca19a46 100644 --- a/src/main/java/li/strolch/service/ServiceHandler.java +++ b/src/main/java/li/strolch/service/ServiceHandler.java @@ -18,7 +18,7 @@ package li.strolch.service; import ch.eitchnet.privilege.model.Certificate; public interface ServiceHandler { - + public U doService(Certificate certificate, Service service, T argument); diff --git a/src/main/java/li/strolch/service/ServiceResult.java b/src/main/java/li/strolch/service/ServiceResult.java index 5ce54d75d..5b846b200 100644 --- a/src/main/java/li/strolch/service/ServiceResult.java +++ b/src/main/java/li/strolch/service/ServiceResult.java @@ -19,7 +19,6 @@ import java.io.Serializable; /** * @author Robert von Burg - * */ public class ServiceResult implements Serializable { private static final long serialVersionUID = 1L; @@ -49,6 +48,13 @@ public class ServiceResult implements Serializable { this.throwable = throwable; } + /** + * @return true if the state is {@link ServiceResultState#SUCCESS} + */ + public boolean isOk() { + return this.state == ServiceResultState.SUCCESS; + } + /** * @return the state */ diff --git a/src/main/java/li/strolch/service/model/AddOrderCollectionService.java b/src/main/java/li/strolch/service/model/AddOrderCollectionService.java new file mode 100644 index 000000000..41144c01a --- /dev/null +++ b/src/main/java/li/strolch/service/model/AddOrderCollectionService.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 li.strolch.service.model; + +import java.util.List; + +import li.strolch.model.Order; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.agent.api.OrderMap; +import li.strolch.service.AbstractService; +import li.strolch.service.ServiceArgument; +import li.strolch.service.ServiceResult; + +/** + * @author Robert von Burg + */ +public class AddOrderCollectionService extends + AbstractService { + + private static final long serialVersionUID = 1L; + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(); + } + + @Override + protected ServiceResult internalDoService(AddOrderCollectionArg arg) { + + OrderMap orderMap = getOrderMap(arg.realm); + try (StrolchTransaction tx = orderMap.openTx(arg.realm)) { + for (Order order : arg.orders) { + orderMap.add(tx, order); + } + } + + return ServiceResult.success(); + } + + public static class AddOrderCollectionArg extends ServiceArgument { + private static final long serialVersionUID = 1L; + public List orders; + } +} diff --git a/src/main/java/li/strolch/service/model/AddOrderService.java b/src/main/java/li/strolch/service/model/AddOrderService.java new file mode 100644 index 000000000..af7869bf0 --- /dev/null +++ b/src/main/java/li/strolch/service/model/AddOrderService.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 li.strolch.service.model; + +import li.strolch.model.Order; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.agent.api.OrderMap; +import li.strolch.service.AbstractService; +import li.strolch.service.ServiceArgument; +import li.strolch.service.ServiceResult; + +/** + * @author Robert von Burg + */ +public class AddOrderService extends AbstractService { + + private static final long serialVersionUID = 1L; + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(); + } + + @Override + protected ServiceResult internalDoService(AddOrderArg arg) { + + OrderMap orderMap = getOrderMap(arg.realm); + try (StrolchTransaction tx = orderMap.openTx(arg.realm)) { + orderMap.add(tx, arg.order); + } + + return ServiceResult.success(); + } + + public static class AddOrderArg extends ServiceArgument { + private static final long serialVersionUID = 1L; + public Order order; + } +} diff --git a/src/main/java/li/strolch/service/model/AddResourceCollectionService.java b/src/main/java/li/strolch/service/model/AddResourceCollectionService.java new file mode 100644 index 000000000..377534fb9 --- /dev/null +++ b/src/main/java/li/strolch/service/model/AddResourceCollectionService.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 li.strolch.service.model; + +import java.util.List; + +import li.strolch.model.Resource; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.agent.api.ResourceMap; +import li.strolch.service.AbstractService; +import li.strolch.service.ServiceArgument; +import li.strolch.service.ServiceResult; + +/** + * @author Robert von Burg + */ +public class AddResourceCollectionService extends + AbstractService { + + private static final long serialVersionUID = 1L; + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(); + } + + @Override + protected ServiceResult internalDoService(AddResourceCollectionArg arg) { + + ResourceMap resourceMap = getResourceMap(arg.realm); + try (StrolchTransaction tx = resourceMap.openTx(arg.realm)) { + for (Resource resource : arg.resources) { + resourceMap.add(tx, resource); + } + } + + return ServiceResult.success(); + } + + public static class AddResourceCollectionArg extends ServiceArgument { + private static final long serialVersionUID = 1L; + public List resources; + } +} diff --git a/src/main/java/li/strolch/service/model/AddResourceService.java b/src/main/java/li/strolch/service/model/AddResourceService.java new file mode 100644 index 000000000..3dc33791a --- /dev/null +++ b/src/main/java/li/strolch/service/model/AddResourceService.java @@ -0,0 +1,52 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 li.strolch.service.model; + +import li.strolch.model.Resource; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.agent.api.ResourceMap; +import li.strolch.service.AbstractService; +import li.strolch.service.ServiceArgument; +import li.strolch.service.ServiceResult; + +/** + * @author Robert von Burg + */ +public class AddResourceService extends AbstractService { + + private static final long serialVersionUID = 1L; + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(); + } + + @Override + protected ServiceResult internalDoService(AddResourceArg arg) { + + ResourceMap resourceMap = getResourceMap(arg.realm); + try (StrolchTransaction tx = resourceMap.openTx(arg.realm)) { + resourceMap.add(tx, arg.resource); + } + + return ServiceResult.success(); + } + + public static class AddResourceArg extends ServiceArgument { + private static final long serialVersionUID = 1L; + public Resource resource; + } +} diff --git a/src/main/java/li/strolch/service/model/ImportModelFromXmlService.java b/src/main/java/li/strolch/service/model/ImportModelFromXmlService.java new file mode 100644 index 000000000..9b4bf5837 --- /dev/null +++ b/src/main/java/li/strolch/service/model/ImportModelFromXmlService.java @@ -0,0 +1,81 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 li.strolch.service.model; + +import java.io.File; +import java.text.MessageFormat; + +import li.strolch.exception.StrolchException; +import li.strolch.model.xml.XmlModelDefaultHandler.XmlModelStatistics; +import li.strolch.model.xml.XmlModelFileHandler; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.runtime.agent.api.OrderMap; +import li.strolch.runtime.agent.api.ResourceMap; +import li.strolch.runtime.agent.impl.InMemoryElementListener; +import li.strolch.service.AbstractService; +import li.strolch.service.ServiceArgument; +import li.strolch.service.ServiceResult; +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public class ImportModelFromXmlService extends + AbstractService { + + private static final long serialVersionUID = 1L; + + @Override + protected ServiceResult getResultInstance() { + return new ServiceResult(); + } + + @Override + protected ServiceResult internalDoService(ImportModelFromXmlArg arg) { + + ResourceMap resourceMap = getResourceMap(arg.realm); + OrderMap orderMap = getOrderMap(arg.realm); + + File dataPath = getRuntimeConfiguration().getDataPath(); + File modelFile = new File(dataPath, arg.fileName); + if (!modelFile.exists()) { + String msg = "Model File does not exist with name {0} in data path {1}"; //$NON-NLS-1$ + msg = MessageFormat.format(msg, arg.fileName, dataPath); + throw new StrolchException(msg); + } + + XmlModelStatistics statistics; + try (StrolchTransaction tx = resourceMap.openTx(arg.realm)) { + InMemoryElementListener elementListener = new InMemoryElementListener(tx, resourceMap, orderMap); + XmlModelFileHandler handler = new XmlModelFileHandler(elementListener, modelFile); + handler.parseFile(); + statistics = handler.getStatistics(); + } + + String durationS = StringHelper.formatNanoDuration(statistics.durationNanos); + logger.info(MessageFormat.format( + "Loading XML Model file {0} for realm {1} took {2}.", modelFile.getName(), arg.realm, durationS)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$ + logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$ + + return ServiceResult.success(); + } + + public static class ImportModelFromXmlArg extends ServiceArgument { + private static final long serialVersionUID = 1L; + public String fileName; + } +}