KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > plugin > PluginRepositoryDownloader


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  */

39 public class PluginRepositoryDownloader implements PluginRepositoryList {
40     private final static Log log = LogFactory.getLog(PluginRepositoryDownloader.class);
41     private List JavaDoc downloadRepositories = new ArrayList JavaDoc();
42     private List JavaDoc userRepositories = new ArrayList JavaDoc();
43     private Kernel kernel;
44     private AbstractName name;
45     private URL JavaDoc repositoryList;
46
47     public PluginRepositoryDownloader(List JavaDoc downloadRepositories, List JavaDoc userRepositories, URL JavaDoc 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 JavaDoc downloadRepositories) {
59         this.downloadRepositories = downloadRepositories;
60         if(this.downloadRepositories == null) this.downloadRepositories = new ArrayList JavaDoc();
61     }
62
63     /**
64      * Any repositories that the user added manually
65      */

66     public void setUserRepositories(List JavaDoc userRepositories) {
67         this.userRepositories = userRepositories;
68         if(this.userRepositories == null) this.userRepositories = new ArrayList JavaDoc();
69     }
70
71     /**
72      * Gets the union of centrally-listed repositories and user-added repositories.
73      */

74     public URL JavaDoc[] getRepositories() {
75         List JavaDoc list = new ArrayList JavaDoc();
76         for (int i = 0; i < downloadRepositories.size(); i++) {
77             String JavaDoc url = (String JavaDoc) downloadRepositories.get(i);
78             try {
79                 list.add(new URL JavaDoc(url.trim()));
80             } catch (MalformedURLException JavaDoc 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 JavaDoc url = (String JavaDoc) userRepositories.get(i);
86             try {
87                 list.add(new URL JavaDoc(url.trim()));
88             } catch (MalformedURLException JavaDoc e) {
89                 log.error("Unable to format plugin repository URL "+url, e);
90             }
91         }
92         return (URL JavaDoc[]) list.toArray(new URL JavaDoc[list.size()]);
93     }
94
95     /**
96      * Go download a fresh copy of the repository list.
97      */

98     public void refresh() {
99         BufferedReader JavaDoc in = null;
100         try {
101             in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(repositoryList.openStream()));
102             String JavaDoc line;
103             List JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc e) {
114             log.error("Unable to save download repositories", e);
115         } finally {
116             if (in != null) {
117                 try {
118                     in.close();
119                 } catch (IOException JavaDoc ignored) {}
120             }
121         }
122     }
123
124     /**
125      * Adds a new repository that the user put in manually.
126      */

127     public void addUserRepository(URL JavaDoc repo) {
128         userRepositories.add(repo.toString());
129         try {
130             kernel.setAttribute(name, "userRepositories", userRepositories);
131         } catch (Exception JavaDoc 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 JavaDoc.class, true);
142         infoFactory.addAttribute("userRepositories", List JavaDoc.class, true);
143         infoFactory.addAttribute("repositoryList", URL JavaDoc.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 JavaDoc[]{"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