[New] Added new services
AddResource*Service, AddOrder*Service, ImportModelFromXmlService
This commit is contained in:
parent
8408e5e62a
commit
cb89abd469
|
@ -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<T extends ServiceArgument, U extends Servi
|
|||
protected static final Logger logger = LoggerFactory.getLogger(AbstractService.class);
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private ComponentContainer container;
|
||||
private DefaultServiceHandler serviceHandler;
|
||||
|
||||
/**
|
||||
* @return the container
|
||||
* @param serviceHandler
|
||||
* the serviceHandler to set
|
||||
*/
|
||||
protected ComponentContainer getContainer() {
|
||||
return this.container;
|
||||
public void setServiceHandler(DefaultServiceHandler serviceHandler) {
|
||||
this.serviceHandler = serviceHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContainer(ComponentContainer container) {
|
||||
this.container = container;
|
||||
public <V> V getComponent(Class<V> 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
|
||||
|
|
|
@ -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 <eitch@eitchnet.ch>
|
||||
*
|
||||
*/
|
||||
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> T getComponent(Class<T> 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 extends ServiceResult> U doService(Certificate certificate, Service<ServiceArgument, U> 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$
|
||||
|
|
|
@ -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<T extends ServiceArgument, U extends ServiceResult> extends Serializable, Restrictable {
|
||||
|
||||
public U doService(T argument);
|
||||
|
||||
public void setContainer(ComponentContainer container);
|
||||
}
|
||||
|
|
|
@ -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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class ServiceArgument implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// marker interface
|
||||
/**
|
||||
* <p>
|
||||
* Set this to the realm in which the service should operate
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* realm = StrolchConstants.DEFAULT_REALM
|
||||
* </p>
|
||||
*/
|
||||
public String realm = StrolchConstants.DEFAULT_REALM;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ package li.strolch.service;
|
|||
import ch.eitchnet.privilege.model.Certificate;
|
||||
|
||||
public interface ServiceHandler {
|
||||
|
||||
|
||||
public <T extends ServiceArgument, U extends ServiceResult> U doService(Certificate certificate,
|
||||
Service<T, U> service, T argument);
|
||||
|
||||
|
|
|
@ -19,7 +19,6 @@ import java.io.Serializable;
|
|||
|
||||
/**
|
||||
* @author Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
*/
|
||||
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
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class AddOrderCollectionService extends
|
||||
AbstractService<AddOrderCollectionService.AddOrderCollectionArg, ServiceResult> {
|
||||
|
||||
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<Order> orders;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class AddOrderService extends AbstractService<AddOrderService.AddOrderArg, ServiceResult> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class AddResourceCollectionService extends
|
||||
AbstractService<AddResourceCollectionService.AddResourceCollectionArg, ServiceResult> {
|
||||
|
||||
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<Resource> resources;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class AddResourceService extends AbstractService<AddResourceService.AddResourceArg, ServiceResult> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
|
||||
*
|
||||
* 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 <eitch@eitchnet.ch>
|
||||
*/
|
||||
public class ImportModelFromXmlService extends
|
||||
AbstractService<ImportModelFromXmlService.ImportModelFromXmlArg, ServiceResult> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue