// reattach to make sure it is in the right order
router.detach(appInfo.app);
Term match = entry.getTerm(T_APP_MATCH);
if (match!=null && match.getValues()!=null) {
for (String pattern : match.getValues()) {
TemplateRoute route = router.attach(pattern,appInfo.app);
if (exact) {
route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
}
}
} else {
TemplateRoute route = router.attach("",appInfo.app);
if (exact) {
route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
}
}
getLogger().info("No changes, skipping.");
continue;
}
for (Term t : entry.getTerms().values()) {
getLogger().info(t.getURI()+": "+t.getFirstValue());
}
getLogger().info(T_APP_MATCH.toString());
Term classTerm = entry.getTerm(T_APP_CLASS);
Term proxyTerm = entry.getTerm(T_APP_PROXY);
Context appContext = context.createChildContext();
LinkSet set = new LinkSet();
set.addLinkSet(entry.getLinks());
set.addLinkSet(hostConf.getLinks());
appContext.getAttributes().put(WebComponent.LINKS_ATTR,set);
appContext.getAttributes().put(ScriptManager.ATTR,scriptManager);
appContext.getAttributes().put(ResourceManager.ATTR,resourceManager);
for (URI t : entry.getTerms().keySet()) {
String value = entry.getTerm(t).getFirstValue();
getLogger().info("Setting parameter: "+t+"="+value);
appContext.getParameters().set(t.toString(),value,false);
}
appContext.getParameters().set("username",autoConf.getUsername(),false);
appContext.getParameters().set("password",autoConf.getPassword(),false);
for (Term t : entry.getTerms().values()) {
String key = t.getURI().toString();
if (t.getValues()!=null) {
for (String value : t.getValues()) {
appContext.getParameters().add(key, value);
}
} else {
appContext.getParameters().add(key,"true");
}
}
Application app = null;
if (proxyTerm!=null) {
String value = proxyTerm.getFirstValue();
List<Link> links = entry.getLinks().get(value);
Link target = null;
if (links!=null && links.size()>0) {
target = links.get(0);
}
if (target==null) {
links = hostConf.getLinks().get(value);
if (links!=null && links.size()>0) {
target = links.get(0);
}
}
if (target!=null) {
app = new ProxyApplication(appContext,target);
}
} else if (classTerm!=null) {
String className = classTerm.getFirstValue();
List<Link> libraryLinks = entry.getLinks().get("library");
String href = null;
Text text = entry.getContent();
if (text!=null) {
href = text.getSourceLink();
}
Class<Application> appClass = null;
Class<?> foundClass = null;
if ((libraryLinks==null || libraryLinks.size()==0) && href==null) {
foundClass = Class.forName(className);
} else {
URL [] downloads = new URL[(libraryLinks==null ? 0 : libraryLinks.size())+(href==null ? 0 : 1)];
if (href!=null) {
downloads[0] = entry.getDocument().getDocumentElement().getBaseURI().resolve(href).toURL();
}
if (libraryLinks!=null) {
for (int pos = href==null ? 0 : 1; pos<libraryLinks.size(); pos++) {
Link link = libraryLinks.get(pos);
downloads[pos] = link.getLink().toURL();
}
}
URL [] urls = new URL[downloads.length];
for (int i=0; i<downloads.length; i++) {
File jarfile = File.createTempFile("jar-T"+System.currentTimeMillis()+"-", ".jar");
urls[i] = jarfile.toURL();
URLRetriever retriever = new URLRetriever(downloads[i]);
try {
retriever.retrieve(jarfile, autoConf.getUsername(), autoConf.getPassword());
} catch (IOException ex) {
getLogger().info("Cannot download "+downloads[i]+" due to: "+ex.getMessage());
continue;
}
}
URLClassLoader classLoader = new URLClassLoader(urls,ConfiguredHost.class.getClassLoader()) {
protected PermissionCollection getPermissions(CodeSource source) {
PermissionCollection collection = super.getPermissions(source);
URL url = source.getLocation();
getLogger().info("Code source: "+url);
Enumeration<Permission> permissions = collection.elements();
while (permissions.hasMoreElements()) {
Permission p = permissions.nextElement();
getLogger().info("Permission: "+p.getName()+", "+p.getClass().getName()+", action: "+p.getActions());
}
return collection;
}
};
foundClass = classLoader.loadClass(className);
PermissionCollection collection = foundClass.getProtectionDomain().getPermissions();
Enumeration<Permission> permissions = collection.elements();
while (permissions.hasMoreElements()) {
Permission p = permissions.nextElement();
getLogger().info("Permission: "+p.getName()+", "+p.getClass().getName()+", action: "+p.getActions());
}
}
if (!Application.class.isAssignableFrom(foundClass)) {
getLogger().info("Class "+className+" is not an subclas of "+Application.class.getName());
continue;
}
appClass = (Class<Application>)foundClass;
Constructor<Application> makeit = appClass.getConstructor(Context.class);
app = makeit.newInstance(appContext);
}
if (app!=null) {
if (appInfo!=null) {
router.detach(appInfo.app);
appInfo.app = app;
appInfo.edited = entry.getEdited();
Term match = entry.getTerm(T_APP_MATCH);
if (match!=null && match.getValues()!=null) {
for (String pattern : match.getValues()) {
TemplateRoute route = router.attach(pattern,app);
if (exact) {
route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
}
}
} else {
TemplateRoute route = router.attach("",app);
if (exact) {
route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
}
}
} else {
applications.put(entry.getId(),new AppInfo(app,entry.getEdited()));
Term match = entry.getTerm(T_APP_MATCH);
if (match!=null && match.getValues()!=null) {
for (String pattern : match.getValues()) {
TemplateRoute route = router.attach(pattern,app);
if (exact) {
route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
}
}
} else {
TemplateRoute route = router.attach("",app);
if (exact) {
route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
}
}
}
}