1 17 package org.apache.geronimo.system.plugin; 18 19 import java.net.URL ; 20 import java.net.MalformedURLException ; 21 import java.util.List ; 22 import java.util.ArrayList ; 23 import java.io.BufferedReader ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.apache.geronimo.kernel.Kernel; 29 import org.apache.geronimo.gbean.AbstractName; 30 import org.apache.geronimo.gbean.GBeanInfo; 31 import org.apache.geronimo.gbean.GBeanInfoBuilder; 32 33 39 public class PluginRepositoryDownloader implements PluginRepositoryList { 40 private final static Log log = LogFactory.getLog(PluginRepositoryDownloader.class); 41 private List downloadRepositories = new ArrayList (); 42 private List userRepositories = new ArrayList (); 43 private Kernel kernel; 44 private AbstractName name; 45 private URL repositoryList; 46 47 public PluginRepositoryDownloader(List downloadRepositories, List userRepositories, URL repositoryList, Kernel kernel, AbstractName name) { 48 if(downloadRepositories != null) this.downloadRepositories = downloadRepositories; 49 if(userRepositories != null) this.userRepositories = userRepositories; 50 this.repositoryList = repositoryList; 51 this.kernel = kernel; 52 this.name = name; 53 } 54 55 58 public void setDownloadRepositories(List downloadRepositories) { 59 this.downloadRepositories = downloadRepositories; 60 if(this.downloadRepositories == null) this.downloadRepositories = new ArrayList (); 61 } 62 63 66 public void setUserRepositories(List userRepositories) { 67 this.userRepositories = userRepositories; 68 if(this.userRepositories == null) this.userRepositories = new ArrayList (); 69 } 70 71 74 public URL [] getRepositories() { 75 List list = new ArrayList (); 76 for (int i = 0; i < downloadRepositories.size(); i++) { 77 String url = (String ) downloadRepositories.get(i); 78 try { 79 list.add(new URL (url.trim())); 80 } catch (MalformedURLException e) { 81 log.error("Unable to format plugin repository URL "+url, e); 82 } 83 } 84 for (int i = 0; i < userRepositories.size(); i++) { 85 String url = (String ) userRepositories.get(i); 86 try { 87 list.add(new URL (url.trim())); 88 } catch (MalformedURLException e) { 89 log.error("Unable to format plugin repository URL "+url, e); 90 } 91 } 92 return (URL []) list.toArray(new URL [list.size()]); 93 } 94 95 98 public void refresh() { 99 BufferedReader in = null; 100 try { 101 in = new BufferedReader (new InputStreamReader (repositoryList.openStream())); 102 String line; 103 List list = new ArrayList (); 104 while((line = in.readLine()) != null) { 105 line = line.trim(); 106 if(!line.equals("") && !line.startsWith("#")) { 107 list.add(line); 108 } 109 } 110 in.close(); 111 in = null; 112 kernel.setAttribute(name, "downloadRepositories", list); 113 } catch (Exception e) { 114 log.error("Unable to save download repositories", e); 115 } finally { 116 if (in != null) { 117 try { 118 in.close(); 119 } catch (IOException ignored) {} 120 } 121 } 122 } 123 124 127 public void addUserRepository(URL repo) { 128 userRepositories.add(repo.toString()); 129 try { 130 kernel.setAttribute(name, "userRepositories", userRepositories); 131 } catch (Exception e) { 132 log.error("Unable to save user repositories", e); 133 } 134 } 135 136 public static final GBeanInfo GBEAN_INFO; 137 138 static { 139 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(PluginRepositoryDownloader.class); 140 141 infoFactory.addAttribute("downloadRepositories", List .class, true); 142 infoFactory.addAttribute("userRepositories", List .class, true); 143 infoFactory.addAttribute("repositoryList", URL .class, true); 144 infoFactory.addAttribute("kernel", Kernel.class, false); 145 infoFactory.addAttribute("abstractName", AbstractName.class, false); 146 infoFactory.addInterface(PluginRepositoryList.class); 147 infoFactory.setConstructor(new String []{"downloadRepositories","userRepositories","repositoryList","kernel","abstractName"}); 148 149 GBEAN_INFO = infoFactory.getBeanInfo(); 150 } 151 152 public static GBeanInfo getGBeanInfo() { 153 return GBEAN_INFO; 154 } 155 } 156 | Popular Tags |