Skip to content

Commit

Permalink
extract QBean descriptors from jar
Browse files Browse the repository at this point in the history
optional resources in META-INF/q2/deploy and
META-INF/q2/cfg are extracted and deployed
  • Loading branch information
ar committed Jul 15, 2017
1 parent 3459e48 commit 1dc8a15
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions jpos/src/main/java/org/jpos/q2/Q2.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.jdom2.output.XMLOutputter;
import org.jpos.iso.ISOException;
import org.jpos.iso.ISOUtil;
import org.jpos.q2.install.ModuleUtils;
import org.jpos.q2.ssh.SshService;
import org.jpos.security.SystemSeed;
import org.jpos.util.Log;
Expand All @@ -47,7 +48,7 @@
import org.osgi.framework.BundleException;
import org.osgi.framework.launch.Framework;
import org.osgi.framework.launch.FrameworkFactory;

import org.xml.sax.SAXException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.management.InstanceAlreadyExistsException;
Expand Down Expand Up @@ -129,7 +130,9 @@ public class Q2 implements FileFilter, Runnable {
private String sshAuthorizedKeys;
private String sshUser;
private String sshHostKeyFile;

private static String DEPLOY_PREFIX = "META-INF/q2/deploy/";
private static String CFG_PREFIX = "META-INF/q2/cfg/";

public Q2 (String[] args, BundleContext bundleContext) {
super();
this.args = args;
Expand Down Expand Up @@ -233,6 +236,7 @@ public Object run() {
"05_sshd-" + getInstanceId() + ".xml", false, true);
}

deployInternal();
for (int i = 1; !shutdown; i++) {
try {
boolean forceNewClassLoader = scan() && i > 1;
Expand All @@ -249,6 +253,7 @@ public Object run() {
q2Thread.setContextClassLoader(loader);
}
logVersion();

deploy();
checkModified();
if (!waitForChanges(service))
Expand Down Expand Up @@ -768,7 +773,7 @@ private void deployBundle (File bundle, boolean encrypt)
public void deployElement (Element e, String fileName, boolean encrypt, boolean isTransient)
throws ISOException, IOException, GeneralSecurityException
{
e = (Element) e.clone ();
e = e.clone ();

XMLOutputter out = new XMLOutputter (Format.getPrettyFormat());
Document doc = new Document ();
Expand Down Expand Up @@ -1098,4 +1103,46 @@ private void registerQ2() {
}
}
}

private void deployInternal() throws IOException, JDOMException, SAXException, ISOException, GeneralSecurityException {
extractCfg();
extractDeploy();
}
private void extractCfg() throws IOException {
List<String> qbeans = ModuleUtils.getModuleEntries(CFG_PREFIX);
for (String resource : qbeans)
copyResourceToFile(resource, new File("cfg", resource.substring(CFG_PREFIX.length())));
}
private void extractDeploy() throws IOException, JDOMException, SAXException, ISOException, GeneralSecurityException {
List<String> qbeans = ModuleUtils.getModuleEntries(DEPLOY_PREFIX);
for (String resource : qbeans) {
if (resource.toLowerCase().endsWith(".xml"))
deployResource(resource);
else
copyResourceToFile(resource, new File("cfg", resource.substring(DEPLOY_PREFIX.length())));

}
}

private void copyResourceToFile(String resource, File destination) throws IOException {
// taken from @vsalaman's Install using human readable braces as God mandates
try (InputStream source = getClass().getClassLoader().getResourceAsStream(resource)) {
try (FileOutputStream output = new FileOutputStream(destination)) {
int n;
byte[] buffer = new byte[4096];
while (-1 != (n = source.read(buffer))) {
output.write(buffer, 0, n);
}
}
}
}
private void deployResource(String resource)
throws IOException, SAXException, JDOMException, GeneralSecurityException, ISOException
{
SAXBuilder builder = new SAXBuilder();
try (InputStream source = getClass().getClassLoader().getResourceAsStream(resource)) {
Document doc = builder.build(source);
deployElement (doc.getRootElement(), resource.substring(DEPLOY_PREFIX.length()), false,true);
}
}
}

0 comments on commit 1dc8a15

Please sign in to comment.