1 16 package org.apache.cocoon.forms.binding.library; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.cocoon.forms.binding.Binding; 25 import org.apache.cocoon.forms.binding.BindingManager; 26 import org.apache.cocoon.forms.binding.JXPathBindingManager; 27 import org.apache.cocoon.forms.util.DomHelper; 28 import org.apache.cocoon.util.location.LocationAttributes; 29 import org.apache.commons.lang.StringUtils; 30 import org.w3c.dom.Element ; 31 32 36 public class Library { 37 38 public static final String SEPARATOR = ":"; 39 40 protected LibraryManager manager = null; 42 43 protected Map definitions = new HashMap (); 45 protected Map inclusions = new HashMap (); 46 47 protected Object shared = new Object (); 49 50 protected String sourceURI = null; 51 protected JXPathBindingManager.Assistant assistant = null; 52 53 public Library(ServiceManager sm) throws ServiceException { 54 manager = (LibraryManager)sm.lookup(LibraryManager.ROLE); 55 } 56 57 public Library(LibraryManager lm) { 58 manager = lm; 59 } 60 61 public void setAssistant(JXPathBindingManager.Assistant assistant) { 62 this.assistant = assistant; 63 } 64 65 public void setSourceURI(String uri) { 66 sourceURI = uri; 67 } 68 public String getSourceURI() { 69 return sourceURI; 70 } 71 72 public boolean dependenciesHaveChanged() throws Exception { 73 74 Iterator it = this.inclusions.values().iterator(); 75 while(it.hasNext()) { 76 Dependency dep = (Dependency)it.next(); 77 if(!dep.isValid()) 78 return true; 79 } 80 81 return false; 82 } 83 84 92 public boolean includeAs(String key, String librarysource) 93 throws LibraryException 94 { 95 try { 96 if( (!inclusions.containsKey(key) || key.indexOf(SEPARATOR)>-1) 98 && manager.getLibrary(librarysource, sourceURI)!=null) { 99 inclusions.put(key,new Dependency(librarysource)); 100 return true; 101 } 102 return false; 103 } catch(Exception e) { 104 throw new LibraryException("Could not include library '"+librarysource+"'",e); 105 } 106 107 } 108 109 public Binding getBinding(String key) throws LibraryException { 110 111 String librarykey = null; 112 String definitionkey = key; 113 114 if(key.indexOf(SEPARATOR)>-1) { 115 String [] parts = StringUtils.split(key,SEPARATOR); 116 librarykey = parts[0]; 117 definitionkey = parts[1]; 118 for(int i=2; i<parts.length; i++) { 119 definitionkey += SEPARATOR+parts[i]; 120 } 121 } 122 123 if(librarykey!=null) { 124 if(inclusions.containsKey(librarykey)) { 125 try { 126 return manager.getLibrary(((Dependency)inclusions.get(librarykey)).dependencySourceURI, sourceURI).getBinding(definitionkey); 127 } catch(Exception e) { 128 throw new LibraryException("Couldn't get Library key='"+librarykey+"' source='"+inclusions.get(librarykey)+"",e); 129 } 130 } else { 131 throw new LibraryException("Library '"+librarykey+"' does not exist! (lookup: '"+key+"')"); 132 } 133 } else { 134 return (Binding)definitions.get(definitionkey); 135 } 136 } 137 138 public void buildLibrary(Element libraryElement) throws Exception { 139 sourceURI = LocationAttributes.getURI(libraryElement); 140 this.assistant.getContext().setLocalLibrary(this); 141 Element [] bindingElements = DomHelper.getChildElements(libraryElement, BindingManager.NAMESPACE); 142 for (int i = 0; i < bindingElements.length; i++) { 143 Element bindingElement = bindingElements[i]; 144 Binding binding = this.assistant.getBindingForConfigurationElement(bindingElement); 145 addBinding(binding); 146 } 147 } 148 149 public void addBinding(Binding binding) throws LibraryException { 150 if(definitions.containsKey(binding.getId())) 151 throw new LibraryException("Library already contains a binding with this ID!"); 152 153 definitions.put(binding.getId(),binding); 154 manager.debug(this+": Put binding with id: "+binding.getId()); 155 } 156 157 158 164 public class Dependency { 165 166 private String dependencySourceURI; 167 private Object shared; 168 169 public Dependency(String dependencySourceURI) throws Exception { 170 this.dependencySourceURI = dependencySourceURI; 171 172 Library lib = manager.getLibrary(this.dependencySourceURI,sourceURI); 173 this.shared = lib.shared; 174 } 175 176 public boolean isValid() throws LibraryException { 177 try { 178 179 if(manager.libraryInCache(dependencySourceURI,sourceURI)) { 180 Library lib = manager.getLibrary(dependencySourceURI,sourceURI); 181 182 if(this.shared == lib.shared) 183 return true; 184 } 185 186 return false; 187 } catch(Exception forward) { 188 throw new LibraryException("Exception occured while checking dependency validity!",forward); 189 } 190 191 } 192 } 193 194 } 195 | Popular Tags |