View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
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  /**
34   * An implementation of PluginRepositoryList that downloads plugins from
35   * an Apache web site.
36   *
37   * @version $Rev: 412620 $ $Date: 2006-06-07 17:04:49 -0700 (Wed, 07 Jun 2006) $
38   */
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      /**
56       * The list of repositories that were downloaded from central.
57       */
58      public void setDownloadRepositories(List downloadRepositories) {
59          this.downloadRepositories = downloadRepositories;
60          if(this.downloadRepositories == null) this.downloadRepositories = new ArrayList();
61      }
62  
63      /**
64       * Any repositories that the user added manually
65       */
66      public void setUserRepositories(List userRepositories) {
67          this.userRepositories = userRepositories;
68          if(this.userRepositories == null) this.userRepositories = new ArrayList();
69      }
70  
71      /**
72       * Gets the union of centrally-listed repositories and user-added repositories.
73       */
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      /**
96       * Go download a fresh copy of the repository list.
97       */
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     /**
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 }