Richard S. Hall | 809aa75 | 2006-04-04 13:24:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 17 | package org.apache.felix.bundlerepository; |
| 18 | |
| 19 | import java.io.*; |
Richard S. Hall | 21ac64c | 2006-04-24 15:02:40 +0000 | [diff] [blame] | 20 | import java.lang.reflect.Method; |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 21 | import java.net.*; |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 22 | import java.text.ParseException; |
| 23 | import java.text.SimpleDateFormat; |
| 24 | import java.util.Arrays; |
| 25 | |
| 26 | import org.apache.felix.bundlerepository.metadataparser.XmlCommonHandler; |
| 27 | import org.apache.felix.bundlerepository.metadataparser.kxmlsax.KXml2SAXParser; |
| 28 | import org.osgi.service.obr.*; |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 29 | |
| 30 | public class RepositoryImpl implements Repository |
| 31 | { |
| 32 | private String m_name = null; |
| 33 | private long m_lastmodified = 0; |
| 34 | private URL m_url = null; |
| 35 | private Resource[] m_resources = null; |
| 36 | private int m_hopCount = 1; |
| 37 | |
| 38 | // Reusable comparator for sorting resources by name. |
| 39 | private ResourceComparator m_nameComparator = new ResourceComparator(); |
| 40 | |
| 41 | public RepositoryImpl(URL url) |
| 42 | { |
| 43 | m_url = url; |
| 44 | parseRepositoryFile(m_hopCount); |
| 45 | } |
| 46 | |
| 47 | public URL getURL() |
| 48 | { |
| 49 | return m_url; |
| 50 | } |
| 51 | |
| 52 | protected void setURL(URL url) |
| 53 | { |
| 54 | m_url = url; |
| 55 | } |
| 56 | |
| 57 | public Resource[] getResources() |
| 58 | { |
| 59 | return m_resources; |
| 60 | } |
| 61 | |
| 62 | // TODO: OBR - Wrong parameter type from metadata parser. |
Richard S. Hall | 21ac64c | 2006-04-24 15:02:40 +0000 | [diff] [blame] | 63 | public void addResource(Resource resource) |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 64 | { |
| 65 | // Set resource's repository. |
| 66 | ((ResourceImpl) resource).setRepository(this); |
| 67 | |
| 68 | // Add to resource array. |
| 69 | if (m_resources == null) |
| 70 | { |
| 71 | m_resources = new Resource[] { resource }; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | Resource[] newResources = new Resource[m_resources.length + 1]; |
| 76 | System.arraycopy(m_resources, 0, newResources, 0, m_resources.length); |
| 77 | newResources[m_resources.length] = resource; |
| 78 | m_resources = newResources; |
| 79 | } |
| 80 | |
| 81 | Arrays.sort(m_resources, m_nameComparator); |
| 82 | } |
| 83 | |
| 84 | public String getName() |
| 85 | { |
| 86 | return m_name; |
| 87 | } |
| 88 | |
| 89 | public void setName(String name) |
| 90 | { |
| 91 | m_name = name; |
| 92 | } |
| 93 | |
| 94 | public long getLastModified() |
| 95 | { |
| 96 | return m_lastmodified; |
| 97 | } |
| 98 | |
| 99 | public void setLastmodified(String s) |
| 100 | { |
| 101 | SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss.SSS"); |
| 102 | try |
| 103 | { |
| 104 | m_lastmodified = format.parse(s).getTime(); |
| 105 | } |
| 106 | catch (ParseException ex) |
| 107 | { |
| 108 | } |
| 109 | } |
| 110 | |
Richard S. Hall | 7a74324 | 2006-04-25 13:14:38 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Default setter method when setting parsed data from the XML file, |
| 113 | * which currently ignores everything. |
| 114 | **/ |
| 115 | protected Object put(Object key, Object value) |
| 116 | { |
| 117 | // Ignore everything for now. |
| 118 | return null; |
| 119 | } |
| 120 | |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 121 | private void parseRepositoryFile(int hopCount) |
| 122 | { |
| 123 | // TODO: OBR - Implement hop count. |
| 124 | InputStream is = null; |
| 125 | BufferedReader br = null; |
| 126 | |
| 127 | try |
| 128 | { |
| 129 | // Do it the manual way to have a chance to |
| 130 | // set request properties as proxy auth (EW). |
| 131 | URLConnection conn = m_url.openConnection(); |
| 132 | |
| 133 | // Support for http proxy authentication |
| 134 | String auth = System.getProperty("http.proxyAuth"); |
| 135 | if ((auth != null) && (auth.length() > 0)) |
| 136 | { |
| 137 | if ("http".equals(m_url.getProtocol()) || |
| 138 | "https".equals(m_url.getProtocol())) |
| 139 | { |
| 140 | String base64 = Util.base64Encode(auth); |
| 141 | conn.setRequestProperty( |
| 142 | "Proxy-Authorization", "Basic " + base64); |
| 143 | } |
| 144 | } |
| 145 | is = conn.getInputStream(); |
| 146 | |
| 147 | // Create the parser Kxml |
| 148 | XmlCommonHandler handler = new XmlCommonHandler(); |
| 149 | try |
| 150 | { |
| 151 | Object factory = new Object() { |
| 152 | public RepositoryImpl newInstance() |
| 153 | { |
| 154 | return RepositoryImpl.this; |
| 155 | } |
| 156 | }; |
Richard S. Hall | 21ac64c | 2006-04-24 15:02:40 +0000 | [diff] [blame] | 157 | |
Richard S. Hall | 7a74324 | 2006-04-25 13:14:38 +0000 | [diff] [blame] | 158 | // Get default setter method for Repository. |
| 159 | Method repoSetter = RepositoryImpl.class.getDeclaredMethod( |
| 160 | "put", new Class[] { Object.class, Object.class }); |
| 161 | |
Richard S. Hall | 21ac64c | 2006-04-24 15:02:40 +0000 | [diff] [blame] | 162 | // Get default setter method for Resource. |
Richard S. Hall | 7a74324 | 2006-04-25 13:14:38 +0000 | [diff] [blame] | 163 | Method resSetter = ResourceImpl.class.getDeclaredMethod( |
Richard S. Hall | 21ac64c | 2006-04-24 15:02:40 +0000 | [diff] [blame] | 164 | "put", new Class[] { Object.class, Object.class }); |
| 165 | |
| 166 | // Map XML tags to types. |
Richard S. Hall | 7a74324 | 2006-04-25 13:14:38 +0000 | [diff] [blame] | 167 | handler.addType("repository", factory, Repository.class, repoSetter); |
| 168 | handler.addType("resource", ResourceImpl.class, Resource.class, resSetter); |
Richard S. Hall | 21ac64c | 2006-04-24 15:02:40 +0000 | [diff] [blame] | 169 | handler.addType("category", CategoryImpl.class, null, null); |
| 170 | handler.addType("require", RequirementImpl.class, Requirement.class, null); |
| 171 | handler.addType("capability", CapabilityImpl.class, Capability.class, null); |
| 172 | handler.addType("p", PropertyImpl.class, null, null); |
| 173 | handler.setDefaultType(String.class, null, null); |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 174 | } |
| 175 | catch (Exception ex) |
| 176 | { |
| 177 | System.err.println("RepositoryAdminImpl: " + ex); |
| 178 | } |
| 179 | |
| 180 | br = new BufferedReader(new InputStreamReader(is)); |
| 181 | KXml2SAXParser parser; |
| 182 | try |
| 183 | { |
| 184 | parser = new KXml2SAXParser(br); |
| 185 | parser.parseXML(handler); |
| 186 | } |
| 187 | catch (Exception ex) |
| 188 | { |
| 189 | ex.printStackTrace(); |
| 190 | } |
| 191 | } |
| 192 | catch (MalformedURLException ex) |
| 193 | { |
| 194 | System.err.println("RepositoryAdminImpl: " + ex); |
| 195 | } |
| 196 | catch (IOException ex) |
| 197 | { |
| 198 | System.err.println("RepositoryAdminImpl: " + ex); |
| 199 | } |
| 200 | finally |
| 201 | { |
| 202 | try |
| 203 | { |
| 204 | if (is != null) is.close(); |
| 205 | } |
| 206 | catch (IOException ex) |
| 207 | { |
| 208 | // Not much we can do. |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |