Package org.atomojo.app.client

Examples of org.atomojo.app.client.Term


            Set<String> ids = new TreeSet<String>();
            List<Entry> entries = new ArrayList<Entry>();
            entries.addAll(feed.getEntriesByTerm(T_APP));
            Comparator<Entry> sorter = new Comparator<Entry>() {
               public int compare(Entry A, Entry B) {
                  Term AP = A.getTerm(T_APP_PRIORITY);
                  Term BP = B.getTerm(T_APP_PRIORITY);
                  //getLogger().info("A: "+A.getTitle()+" "+AP+" vs B: "+B.getTitle()+" "+BP);
                  if (BP==null && AP==null) {
                     return 0;
                  } else if (AP!=null && BP==null) {
                     return -1;
                  } else if (AP==null && BP!=null) {
                     return 1;
                  } else {
                     int a = Integer.parseInt(AP.getFirstValue());
                     int b = Integer.parseInt(BP.getFirstValue());
                     return a>b ? -1 : a==b ? 0 : 1;
                  }
               }
               public boolean equals(Object obj) {
                  return obj==this;
               }
            };
            Collections.sort(entries,sorter);
            Collections.reverse(entries);
            for (Entry entry : entries) {
               found = true;
               URI baseURI = entry.getDocument().getBaseURI();
               Term priority = entry.getTerm(T_APP_PRIORITY);
               getLogger().info("Application: title: "+entry.getTitle()+", base: "+baseURI+", priority: "+(priority==null ? "" : priority.getFirstValue()));
               ids.add(entry.getId());
              
               boolean exact = false;
               if (entry.getTerm(T_APP_MATCH_MODE)!=null) {
                  exact = "exact".equals(entry.getTerm(T_APP_MATCH_MODE).getFirstValue());
               }
              
               AppInfo appInfo = applications.get(entry.getId());
               if (appInfo!=null && appInfo.edited.getTime()>=entry.getEdited().getTime()) {
                  // 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);
                           }
                        }
View Full Code Here

TOP

Related Classes of org.atomojo.app.client.Term

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.