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.*; |
| 20 | import java.net.*; |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 21 | import java.text.ParseException; |
| 22 | import java.text.SimpleDateFormat; |
| 23 | import java.util.Arrays; |
| 24 | |
| 25 | import org.apache.felix.bundlerepository.metadataparser.XmlCommonHandler; |
| 26 | import org.apache.felix.bundlerepository.metadataparser.kxmlsax.KXml2SAXParser; |
| 27 | import org.osgi.service.obr.*; |
Richard S. Hall | a2dc451 | 2006-04-04 13:17:11 +0000 | [diff] [blame] | 28 | |
| 29 | public class RepositoryImpl implements Repository |
| 30 | { |
| 31 | private String m_name = null; |
| 32 | private long m_lastmodified = 0; |
| 33 | private URL m_url = null; |
| 34 | private Resource[] m_resources = null; |
| 35 | private int m_hopCount = 1; |
| 36 | |
| 37 | // Reusable comparator for sorting resources by name. |
| 38 | private ResourceComparator m_nameComparator = new ResourceComparator(); |
| 39 | |
| 40 | public RepositoryImpl(URL url) |
| 41 | { |
| 42 | m_url = url; |
| 43 | parseRepositoryFile(m_hopCount); |
| 44 | } |
| 45 | |
| 46 | public URL getURL() |
| 47 | { |
| 48 | return m_url; |
| 49 | } |
| 50 | |
| 51 | protected void setURL(URL url) |
| 52 | { |
| 53 | m_url = url; |
| 54 | } |
| 55 | |
| 56 | public Resource[] getResources() |
| 57 | { |
| 58 | return m_resources; |
| 59 | } |
| 60 | |
| 61 | // TODO: OBR - Wrong parameter type from metadata parser. |
| 62 | public void addResource(ResourceImpl resource) |
| 63 | { |
| 64 | // Set resource's repository. |
| 65 | ((ResourceImpl) resource).setRepository(this); |
| 66 | |
| 67 | // Add to resource array. |
| 68 | if (m_resources == null) |
| 69 | { |
| 70 | m_resources = new Resource[] { resource }; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | Resource[] newResources = new Resource[m_resources.length + 1]; |
| 75 | System.arraycopy(m_resources, 0, newResources, 0, m_resources.length); |
| 76 | newResources[m_resources.length] = resource; |
| 77 | m_resources = newResources; |
| 78 | } |
| 79 | |
| 80 | Arrays.sort(m_resources, m_nameComparator); |
| 81 | } |
| 82 | |
| 83 | public String getName() |
| 84 | { |
| 85 | return m_name; |
| 86 | } |
| 87 | |
| 88 | public void setName(String name) |
| 89 | { |
| 90 | m_name = name; |
| 91 | } |
| 92 | |
| 93 | public long getLastModified() |
| 94 | { |
| 95 | return m_lastmodified; |
| 96 | } |
| 97 | |
| 98 | public void setLastmodified(String s) |
| 99 | { |
| 100 | SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss.SSS"); |
| 101 | try |
| 102 | { |
| 103 | m_lastmodified = format.parse(s).getTime(); |
| 104 | } |
| 105 | catch (ParseException ex) |
| 106 | { |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private void parseRepositoryFile(int hopCount) |
| 111 | { |
| 112 | // TODO: OBR - Implement hop count. |
| 113 | InputStream is = null; |
| 114 | BufferedReader br = null; |
| 115 | |
| 116 | try |
| 117 | { |
| 118 | // Do it the manual way to have a chance to |
| 119 | // set request properties as proxy auth (EW). |
| 120 | URLConnection conn = m_url.openConnection(); |
| 121 | |
| 122 | // Support for http proxy authentication |
| 123 | String auth = System.getProperty("http.proxyAuth"); |
| 124 | if ((auth != null) && (auth.length() > 0)) |
| 125 | { |
| 126 | if ("http".equals(m_url.getProtocol()) || |
| 127 | "https".equals(m_url.getProtocol())) |
| 128 | { |
| 129 | String base64 = Util.base64Encode(auth); |
| 130 | conn.setRequestProperty( |
| 131 | "Proxy-Authorization", "Basic " + base64); |
| 132 | } |
| 133 | } |
| 134 | is = conn.getInputStream(); |
| 135 | |
| 136 | // Create the parser Kxml |
| 137 | XmlCommonHandler handler = new XmlCommonHandler(); |
| 138 | try |
| 139 | { |
| 140 | Object factory = new Object() { |
| 141 | public RepositoryImpl newInstance() |
| 142 | { |
| 143 | return RepositoryImpl.this; |
| 144 | } |
| 145 | }; |
| 146 | handler.addType("repository", factory, Repository.class); |
| 147 | handler.addType("resource", ResourceImpl.class, Resource.class); |
| 148 | handler.addType("category", CategoryImpl.class, null); |
| 149 | handler.addType("require", RequirementImpl.class, Requirement.class); |
| 150 | handler.addType("capability", CapabilityImpl.class, Capability.class); |
| 151 | handler.addType("p", PropertyImpl.class, null); |
| 152 | handler.setDefaultType(String.class, null); |
| 153 | } |
| 154 | catch (Exception ex) |
| 155 | { |
| 156 | System.err.println("RepositoryAdminImpl: " + ex); |
| 157 | } |
| 158 | |
| 159 | br = new BufferedReader(new InputStreamReader(is)); |
| 160 | KXml2SAXParser parser; |
| 161 | try |
| 162 | { |
| 163 | parser = new KXml2SAXParser(br); |
| 164 | parser.parseXML(handler); |
| 165 | } |
| 166 | catch (Exception ex) |
| 167 | { |
| 168 | ex.printStackTrace(); |
| 169 | } |
| 170 | } |
| 171 | catch (MalformedURLException ex) |
| 172 | { |
| 173 | System.err.println("RepositoryAdminImpl: " + ex); |
| 174 | } |
| 175 | catch (IOException ex) |
| 176 | { |
| 177 | System.err.println("RepositoryAdminImpl: " + ex); |
| 178 | } |
| 179 | finally |
| 180 | { |
| 181 | try |
| 182 | { |
| 183 | if (is != null) is.close(); |
| 184 | } |
| 185 | catch (IOException ex) |
| 186 | { |
| 187 | // Not much we can do. |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |