1 10 11 package org.mmbase.applications.packaging; 12 13 import java.io.BufferedInputStream ; 14 import java.io.File ; 15 import java.net.HttpURLConnection ; 16 import java.net.URL ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 20 import org.mmbase.applications.packaging.providerhandlers.DiskProvider; 21 import org.mmbase.applications.packaging.providerhandlers.HttpProvider; 22 import org.mmbase.applications.packaging.providerhandlers.PackageDiscovery; 23 import org.mmbase.applications.packaging.providerhandlers.ProviderFileWriter; 24 import org.mmbase.applications.packaging.providerhandlers.ProviderInterface; 25 import org.mmbase.applications.packaging.util.ExtendedDocumentReader; 26 import org.mmbase.util.XMLEntityResolver; 27 import org.mmbase.util.logging.Logger; 28 import org.mmbase.util.logging.Logging; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.NamedNodeMap ; 31 import org.xml.sax.InputSource ; 32 33 39 public class ProviderManager { 40 private static Logger log = Logging.getLoggerInstance(ProviderManager.class); 41 private static boolean state = false; 42 private static PackageDiscovery packagediscovery = null; 43 44 private static HashMap providers = null; 46 47 private static HashMap providerhandlers = null; 49 50 51 52 public static final String DTD_PROVIDERHANDLERS_1_0 = "providerhandlers_1_0.dtd"; 53 public static final String DTD_PROVIDERS_1_0 = "providers_1_0.dtd"; 54 public static final String DTD_SHAREAUTOCONFIG_1_0 = "shareautoconfig_1_0.dtd"; 55 56 public static final String PUBLIC_ID_PROVIDERHANDLERS_1_0 = "-//MMBase//DTD providerhandlers config 1.0//EN"; 57 public static final String PUBLIC_ID_PROVIDERS_1_0 = "-//MMBase//DTD providers config 1.0//EN"; 58 public static final String PUBLIC_ID_SHAREAUTOCONFIG_1_0 = "-//MMBase//DTD shareautoconfig 1.0//EN"; 59 60 64 public static void registerPublicIDs() { 65 XMLEntityResolver.registerPublicID(PUBLIC_ID_PROVIDERHANDLERS_1_0, DTD_PROVIDERHANDLERS_1_0, ProviderManager.class); 66 XMLEntityResolver.registerPublicID(PUBLIC_ID_PROVIDERS_1_0, DTD_PROVIDERS_1_0, ProviderManager.class); 67 XMLEntityResolver.registerPublicID(PUBLIC_ID_SHAREAUTOCONFIG_1_0, DTD_SHAREAUTOCONFIG_1_0, ProviderManager.class); 68 } 69 70 71 public static synchronized void init() { 72 if (!isRunning()) { 73 readProviderHandlers(); 74 readProviders(); 75 packagediscovery = new PackageDiscovery(); 76 state=true; 77 } 78 } 79 80 public static boolean isRunning() { 81 return state; 82 } 83 84 88 public static Iterator getProviders() { 89 if (providers == null) init(); 90 return providers.values().iterator(); 91 } 92 93 94 98 public static ProviderInterface getProvider(String name) { 99 if (providers == null) init(); 100 Object o = providers.get(name); 101 if (o != null) { 102 return (ProviderInterface)o; 103 } else { 104 return null; 105 } 106 } 107 108 public static void resetSleepCounter() { 109 packagediscovery.resetSleepCounter(); 110 } 111 112 113 114 115 119 public static ProviderInterface get(String name) { 120 if (providers == null) init(); 121 Object o = providers.get(name); 122 if (o != null) { 123 return (ProviderInterface)o; 124 } else { 125 log.error("Can't find provider with name : "+name); 126 return null; 127 } 128 } 129 130 131 public static boolean delete(String name) { 132 if (providers == null) init(); 133 Object o = providers.get(name); 134 if (o != null) { 135 ((ProviderInterface)o).close(); 137 138 ((ProviderInterface)o).signalUpdate(); 141 142 PackageManager.removeOfflinePackages((ProviderInterface)o); 144 145 BundleManager.removeOfflineBundles((ProviderInterface)o); 147 148 providers.remove(name); 150 return true; 151 } else { 152 return false; 153 } 154 } 155 156 public static void readProviders() { 157 providers = new HashMap (); 158 String filename = PackageManager.getConfigPath()+File.separator+"packaging"+File.separator+"providers.xml"; 159 160 File file = new File (filename); 161 if(file.exists()) { 162 ExtendedDocumentReader reader = new ExtendedDocumentReader(filename,ProviderManager.class); 163 if(reader != null) { 164 for(Iterator ns = reader.getChildElements("providers","provider");ns.hasNext(); ) { 165 Element n = (Element )ns.next(); 166 NamedNodeMap nm = n.getAttributes(); 167 if (nm != null) { 168 String name = null; 169 String method = null; 170 String maintainer = null; 171 172 org.w3c.dom.Node n2 = nm.getNamedItem("name"); 174 if (n2 != null) { 175 name = n2.getNodeValue(); 176 } 177 178 n2 = nm.getNamedItem("method"); 180 if (n2 != null) { 181 method = n2.getNodeValue(); 182 } 183 184 n2 = nm.getNamedItem("maintainer"); 186 if (n2 != null) { 187 maintainer = n2.getNodeValue(); 188 } 189 190 if (method != null) { 191 String classname = (String )providerhandlers.get(method); 193 if (classname != null) { 194 try { 195 Class newclass = Class.forName(classname); 196 ProviderInterface newprovider = (ProviderInterface)newclass.newInstance(); 197 newprovider.init(n,name,method,maintainer); 198 providers.put(name,newprovider); 199 } catch(Exception e) { 200 e.printStackTrace(); 201 } 202 } 203 } 204 } 205 } 206 } 207 } else { 208 log.error("missing package providers file : "+filename); 209 } 210 } 211 212 213 public static boolean writeProviderFile() { 214 ProviderFileWriter.write(); 215 return true; 216 } 217 218 public static String addDiskProvider(String name,String path) { 220 if (get(name) == null) { 221 DiskProvider d = new DiskProvider(); 222 d.init(name,"disk","local",path); 223 providers.put(name,d); 224 } else { 225 String feedback = " '"+name+"' allready in your providers list !"; 226 return feedback; 227 } 228 String feedback = " '"+name+"' added, see overview for details"; 229 return feedback; 230 } 231 232 public static String addSubscribeProvider(String url) { 234 ExtendedDocumentReader reader = getAutoConfigReader(url); 235 if (reader == null) reader = getAutoConfigReader(url+"/mmbase/packagemanager/shareautoconfig.jsp"); 237 if (reader == null) reader = getAutoConfigReader(url+"/mmbase-webapp/mmbase/packagemanager/shareautoconfig.jsp"); 238 if (reader == null) reader = getAutoConfigReader(url+"/shareautoconfig.jsp"); 239 if (reader == null) reader = getAutoConfigReader(url+"/mmbase-webapp/shareautoconfig.jsp"); 240 if (reader != null) { 241 String name = "unknown"; 242 String method = "unknown"; 243 String maintainer = "unknown"; 244 String account = "guest"; 245 String password = "guest"; 246 String path = ""; 247 for(Iterator ns = reader.getChildElements("shareautoconfig");ns.hasNext(); ) { 248 Element e = (Element )ns.next(); 249 String field = e.getNodeName(); 250 org.w3c.dom.Node n = e.getFirstChild(); 251 if (field.equals("name")) name = n.getNodeValue(); 252 if (field.equals("method")) method = n.getNodeValue(); 253 if (field.equals("maintainer")) maintainer = n.getNodeValue(); 254 if (field.equals("account")) account = n.getNodeValue(); 255 if (field.equals("password")) password = n.getNodeValue(); 256 if (field.equals("path")) path = n.getNodeValue(); 257 } 258 if (method.equals("http")) { 259 if (get(name) == null) { 260 ProviderInterface h = new HttpProvider(); 261 h.setAccount(account); 262 h.setPassword(password); 263 h.init(name,method,maintainer,path); 264 providers.put(name,h); 265 } else { 266 String feedback = " '"+name+"' allready in your providers list !"; 267 return feedback; 268 } 269 String feedback = " '"+name+"' added, see overview for details"; 270 return feedback; 271 } 272 } 273 String feedback = "unable to find share autoconfig file at that location"; 274 return feedback; 275 } 276 277 private static ExtendedDocumentReader getAutoConfigReader(String url) { 279 ExtendedDocumentReader reader = null; 280 try { 281 if (url.indexOf("http://") != 0) url = "http://"+url; 282 URL includeURL = new URL (url); 283 HttpURLConnection connection = (HttpURLConnection ) includeURL.openConnection(); 284 BufferedInputStream input = new BufferedInputStream (connection.getInputStream()); 285 reader = new ExtendedDocumentReader(new InputSource (input),ProviderManager.class); 286 return reader; 287 } catch(Exception e) { 288 return null; 289 } 290 } 291 292 293 public static void readProviderHandlers() { 294 providerhandlers = new HashMap (); 295 String filename = PackageManager.getConfigPath()+File.separator+"packaging"+File.separator+"providerhandlers.xml"; 296 297 File file = new File (filename); 298 if(file.exists()) { 299 ExtendedDocumentReader reader = new ExtendedDocumentReader(filename,ProviderManager.class); 300 if(reader != null) { 301 for(Iterator ns = reader.getChildElements("providerhandlers","providerhandler");ns.hasNext(); ) { 302 Element n = (Element )ns.next(); 303 NamedNodeMap nm = n.getAttributes(); 304 if (nm != null) { 305 String method = null; 306 String classname = null; 307 String state = null; 308 String basescore = null; 309 310 org.w3c.dom.Node n2 = nm.getNamedItem("method"); 312 if (n2 != null) { 313 method = n2.getNodeValue(); 314 } 315 316 n2 = nm.getNamedItem("state"); 318 if (n2 != null) { 319 state = n2.getNodeValue(); 320 } 321 322 n2 = nm.getNamedItem("basescore"); 324 if (n2 != null) { 325 basescore = n2.getNodeValue(); 326 } 327 328 n2 = nm.getNamedItem("class"); 330 if (n2 != null) { 331 classname = n2.getNodeValue(); 332 } 333 providerhandlers.put(method,classname); 334 } 335 } 336 } 337 } else { 338 log.error("missing providerhandler file : "+filename); 339 } 340 } 341 342 public static HashMap getProviderHandlers() { 343 return providerhandlers; 344 } 345 346 public static void discoverPackages() { 347 packagediscovery.getPackages(); 348 } 349 350 } 351 | Popular Tags |