info.kwarc.sally4.docmanager.impl.MessageParserImpl Maven / Gradle / Ivy
The newest version!
package info.kwarc.sally4.docmanager.impl;
import info.kwarc.sally4.docmanager.MessageParser;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Validate;
import org.apache.felix.ipojo.extender.Extender;
import org.osgi.framework.Bundle;
import org.osgi.framework.wiring.BundleWiring;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Instantiate
@Provides(specifications=MessageParser.class)
@Extender(
onArrival="onBundleArrival",
onDeparture="onBundleDeparture",
extension="Comm-Package")
public class MessageParserImpl implements MessageParser {
public final static Pattern xmlNamespace = Pattern.compile("xmlns(:(\\w+))?=\"http://kwarc.info/sally/comm/([\\w/:.]+)\"");
final String packagePrefix = "info.kwarc.sally.comm";
class MessageMarshallers {
JAXBContext context;
Marshaller marshaller;
Unmarshaller unmarshaller;
public MessageMarshallers(JAXBContext context) {
this.context = context;
try {
marshaller = context.createMarshaller();
unmarshaller = context.createUnmarshaller();
} catch (JAXBException e) {
e.printStackTrace();
}
}
public JAXBContext getContext() {
return context;
}
public Marshaller getMarshaller() {
return marshaller;
}
public Unmarshaller getUnmarshaller() {
return unmarshaller;
}
}
Logger log;
HashMap bundleMappings = new HashMap();
/* (non-Javadoc)
* @see info.kwarc.sally4.docmanager.impl.MessageParser1#getFormat(java.lang.String)
*/
@Override
public JaxbDataFormat getFormat(String commType) {
return new JaxbDataFormat(bundleMappings.get(commType).getContext());
}
/* (non-Javadoc)
* @see info.kwarc.sally4.docmanager.impl.MessageParser1#getUnmarshaller(java.lang.String)
*/
@Override
public Unmarshaller getUnmarshaller(String schemaType) {
if (!bundleMappings.containsKey(schemaType))
return null;
return bundleMappings.get(schemaType).getUnmarshaller();
}
/* (non-Javadoc)
* @see info.kwarc.sally4.docmanager.impl.MessageParser1#getMarshaller(java.lang.String)
*/
@Override
public Marshaller getMarshaller(String schemaType) {
if (!bundleMappings.containsKey(schemaType))
return null;
return bundleMappings.get(schemaType).getMarshaller();
}
public static String getMessageSchema(String xmlMessage) {
Matcher m = xmlNamespace.matcher(xmlMessage);
if (m.find()) {
return m.group(3);
} else
return null;
}
/* (non-Javadoc)
* @see info.kwarc.sally4.docmanager.impl.MessageParser1#validMessageSchemaDefinition(java.lang.String)
*/
@Override
public boolean validMessageSchemaDefinition(String iface) {
return bundleMappings.containsKey(iface);
}
public T parseAs(String xmlMessage, Class extends T> resultClass) {
String msgType = getMessageSchema(xmlMessage);
if (!bundleMappings.containsKey(msgType))
return null;
StringReader r = new StringReader(xmlMessage);
try {
Object o = bundleMappings.get(msgType).getUnmarshaller().unmarshal(r);
if (resultClass.isAssignableFrom(o.getClass())) {
return (T) o;
}
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
public MessageParserImpl() {
log = LoggerFactory.getLogger(getClass());
}
@Validate
void start() {
try {
// Make sure core is always there
bundleMappings.put("core", new MessageMarshallers(JAXBContext.newInstance("info.kwarc.sally.comm.core", getClass().getClassLoader())));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void onBundleArrival(Bundle bundle, String header) {
log.info("Looking at message definitions in bundle "+bundle.getSymbolicName());
for (String iFace : header.split(",")) {
try {
JAXBContext context = JAXBContext.newInstance(packagePrefix+"."+iFace, bundle.adapt(BundleWiring.class).getClassLoader());
bundleMappings.put(iFace, new MessageMarshallers(context));
log.info("Successfully loaded "+iFace);
} catch (JAXBException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
void onBundleDeparture(Bundle bundle) {
bundleMappings.remove(bundle);
}
@Override
public String marshall(Object obj) {
String pkg = obj.getClass().getPackage().getName();
if (!pkg.startsWith(packagePrefix))
return null;
String schema = pkg.substring(packagePrefix.length()+1);
Marshaller marshaller = getMarshaller(schema);
if (marshaller == null)
return null;
StringWriter output = new StringWriter();
try {
marshaller.marshal(obj, output);
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
return output.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy