001 /** 002 * 003 * Copyright 2005 The Apache Software Foundation 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.geronimo.system.plugin; 018 019 import java.net.URL; 020 import java.net.MalformedURLException; 021 import java.util.List; 022 import java.util.ArrayList; 023 import java.io.BufferedReader; 024 import java.io.IOException; 025 import java.io.InputStreamReader; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 import org.apache.geronimo.kernel.Kernel; 029 import org.apache.geronimo.gbean.AbstractName; 030 import org.apache.geronimo.gbean.GBeanInfo; 031 import org.apache.geronimo.gbean.GBeanInfoBuilder; 032 033 /** 034 * An implementation of PluginRepositoryList that downloads plugins from 035 * an Apache web site. 036 * 037 * @version $Rev: 412620 $ $Date: 2006-06-07 17:04:49 -0700 (Wed, 07 Jun 2006) $ 038 */ 039 public class PluginRepositoryDownloader implements PluginRepositoryList { 040 private final static Log log = LogFactory.getLog(PluginRepositoryDownloader.class); 041 private List downloadRepositories = new ArrayList(); 042 private List userRepositories = new ArrayList(); 043 private Kernel kernel; 044 private AbstractName name; 045 private URL repositoryList; 046 047 public PluginRepositoryDownloader(List downloadRepositories, List userRepositories, URL repositoryList, Kernel kernel, AbstractName name) { 048 if(downloadRepositories != null) this.downloadRepositories = downloadRepositories; 049 if(userRepositories != null) this.userRepositories = userRepositories; 050 this.repositoryList = repositoryList; 051 this.kernel = kernel; 052 this.name = name; 053 } 054 055 /** 056 * The list of repositories that were downloaded from central. 057 */ 058 public void setDownloadRepositories(List downloadRepositories) { 059 this.downloadRepositories = downloadRepositories; 060 if(this.downloadRepositories == null) this.downloadRepositories = new ArrayList(); 061 } 062 063 /** 064 * Any repositories that the user added manually 065 */ 066 public void setUserRepositories(List userRepositories) { 067 this.userRepositories = userRepositories; 068 if(this.userRepositories == null) this.userRepositories = new ArrayList(); 069 } 070 071 /** 072 * Gets the union of centrally-listed repositories and user-added repositories. 073 */ 074 public URL[] getRepositories() { 075 List list = new ArrayList(); 076 for (int i = 0; i < downloadRepositories.size(); i++) { 077 String url = (String) downloadRepositories.get(i); 078 try { 079 list.add(new URL(url.trim())); 080 } catch (MalformedURLException e) { 081 log.error("Unable to format plugin repository URL "+url, e); 082 } 083 } 084 for (int i = 0; i < userRepositories.size(); i++) { 085 String url = (String) userRepositories.get(i); 086 try { 087 list.add(new URL(url.trim())); 088 } catch (MalformedURLException e) { 089 log.error("Unable to format plugin repository URL "+url, e); 090 } 091 } 092 return (URL[]) list.toArray(new URL[list.size()]); 093 } 094 095 /** 096 * Go download a fresh copy of the repository list. 097 */ 098 public void refresh() { 099 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 /** 125 * Adds a new repository that the user put in manually. 126 */ 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 }