org.butor.json.service.DefaultServiceManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of butor-json-server Show documentation
Show all versions of butor-json-server Show documentation
This module enables fast and easy creation of sync., async., req./resp., req./resp. stream HTTP/json services.
It is part of Butor Framework and released under APL 2.0.
The newest version!
/**
* Copyright 2013-2019 Butor Inc.
*
* 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.butor.json.service;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.butor.json.CommonRequestArgs;
import org.butor.json.JsonHelper;
import org.butor.json.JsonServiceRequest;
import org.butor.utils.ApplicationException;
import org.butor.utils.CommonMessageID;
import org.butor.utils.Message;
import org.butor.utils.Message.MessageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.gson.JsonParser;
public class DefaultServiceManager implements ServiceManager,InitializingBean,BeanFactoryAware {
protected Logger logger = LoggerFactory.getLogger(getClass());
private JsonHelper jsh = new JsonHelper();
private JsonParser jsonParser = new JsonParser();
private PlatformTransactionManager transactionManager;
private BeanFactory beanFactory;
private ConcurrentMap cmpRegistry =
new ConcurrentHashMap();
private List components;
private boolean allowHtmlTagsInServicesArgs = true;
@Override
public void registerServices(List components) {
for (ServiceComponent ser : components)
registerService(ser);
}
@Override
public void registerService(ServiceComponent serviceCmp) {
String ns = serviceCmp.getNamespace();
if (cmpRegistry.containsKey(ns)) {
logger.error(String.format("Namespace %s has been used by component %s",
ns, cmpRegistry.get(ns).toString()));
return;
}
cmpRegistry.put(ns, serviceCmp);
}
@Override
public void unregisterService(ServiceComponent cmp) {
cmpRegistry.remove(cmp.getNamespace());
}
@Override
public boolean isBinary(JsonServiceRequest req) {
final ServiceComponent cmp = cmpRegistry.get(req.getNamespace());
if (cmp == null) {
String msg = String.format("No service component found with ns=%s", req.getNamespace());
logger.info(msg);
return false;
}
return cmp.isBinary();
}
@Override
public void invoke(final Context context) {
JsonServiceRequest req = (JsonServiceRequest)context.getRequest();
final String namespace = req.getNamespace();
final ServiceComponent cmp = cmpRegistry.get(namespace);
if (cmp == null) {
String msg = String.format("No service component found with ns=%s", namespace);
logger.info(msg);
return;
}
String args = req.getServiceArgsJson();
if (!allowHtmlTagsInServicesArgs) {
args = args.replaceAll("\\\\u003c|<", "<").replaceAll("\\\\u003e|>", ">");
req.setServiceArgsJson(args);
}
Object[] params = Iterables.toArray(jsonParser.parse(args).getAsJsonArray(), Object.class);
int nbArgs = params.length +1; // +1 for context arg
final String serviceName = req.getService();
final Method serviceMethod = cmp.getService(serviceName, nbArgs);
if (serviceMethod == null) {
String msg = String.format("No service=%s found with ns=%s",
serviceName, namespace);
logger.info(msg);
context.getResponseHandler().addMessage(new Message(0, MessageType.ERROR, msg));
return;
}
try {
final CommonRequestArgs cr = new CommonRequestArgs();
cr.setLang(req.getLang());
cr.setReqId(req.getReqId());
cr.setSessionId(req.getSessionId());
cr.setUserId(req.getUserId());
cr.setDomain(req.getDomain());
Context ictx = new Context() {
@Override
public ResponseHandler> getResponseHandler() {
return context.getResponseHandler();
}
@Override
public CommonRequestArgs getRequest() {
return cr;
}
};
Class>[] pts = serviceMethod.getParameterTypes();
final Object[] serviceParameters = new Object[pts.length];
serviceParameters[0] = ictx;
for (int ii=1; ii getComponents() {
return components;
}
public void setComponents(List components_) {
components = components_;
registerServices(components_);
}
@Override
public void afterPropertiesSet() throws Exception {
try {
transactionManager = beanFactory.getBean(PlatformTransactionManager.class);
} catch (NoSuchBeanDefinitionException e) {
logger.warn("No (or more than one) TransactionManager defined in your Spring context. Will not set transaction manager.",e);
}
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void setAllowHtmlTagsInServicesArgs(boolean allowHtmlTagsInServicesArgs) {
this.allowHtmlTagsInServicesArgs = allowHtmlTagsInServicesArgs;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy