blob: 6d0be6e6ebe5f320ea8d7fd6e9cf8bfd15238b4c [file] [log] [blame]
Stefano Lenzi476013d2007-09-21 23:59:54 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.sandbox.obr.plugin;
20
21import java.io.File;
22import java.net.MalformedURLException;
23import java.net.URI;
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.Iterator;
27import java.util.List;
28import java.util.Map;
29
30import org.apache.maven.plugin.MojoExecutionException;
31import org.osgi.impl.bundle.obr.resource.BundleInfo;
32import org.osgi.impl.bundle.obr.resource.CapabilityImpl;
33import org.osgi.impl.bundle.obr.resource.RepositoryImpl;
34import org.osgi.impl.bundle.obr.resource.RequirementImpl;
35import org.osgi.impl.bundle.obr.resource.ResourceImpl;
36import org.osgi.impl.bundle.obr.resource.VersionImpl;
37
38/**
39 * this class is used to configure bindex and get information built by bindex about targeted bundle.
40 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
41 */
42public class ExtractBindexInfo {
43
44 /**
45 * attribute get from bindex which describe targeted resource.
46 */
47 private ResourceImpl m_resource;
48
49 /**
50 * configure bindex and build information.
51 * @param repoFilename URI on OBR descriptor file
52 * @param outFile path on targeted jar-file
53 * @throws MojoExecutionException occurs if bindex configuration failed
54 */
55 public ExtractBindexInfo(URI repoFilename, String outFile) throws MojoExecutionException {
56
57 this.m_resource = null;
58 RepositoryImpl repository = null;
59 try {
60 repository = new RepositoryImpl(new File(repoFilename).getAbsoluteFile().toURL());
61 } catch (MalformedURLException e) {
62 e.printStackTrace();
63 throw new MojoExecutionException("MalformedURLException");
64 }
65 BundleInfo info = null;
66 try {
67 info = new BundleInfo(repository, new File(outFile));
68 } catch (Exception e) {
69 e.printStackTrace();
70 throw new MojoExecutionException("Exception");
71 }
72
73 try {
74 m_resource = info.build();
75 } catch (Exception e) {
76 e.printStackTrace();
77 throw new MojoExecutionException("Exception");
78 }
79 }
80
81 /**
82 * transform logical operator in xml syntax.
83 * @param filter string which contains logical operator
84 * @return string in correct xml syntax
85 */
86 private String parseFilter(String filter) {
87 filter.replaceAll("&", "&amp");
88 filter.replaceAll(">=", "&gt");
89
90 return filter;
91 }
92
93 /**
94 * extract capabilities from bindex information.
95 * @return bundle capabilities List
96 */
97 public List getCapabilities() {
98 List list = new ArrayList();
99 Collection res = m_resource.getCapabilityList();
100 Iterator it = res.iterator();
101 while (it.hasNext()) {
102 Capability capability = new Capability();
103 CapabilityImpl ci = (CapabilityImpl) it.next();
104 capability.setName(ci.getName());
105 // System.out.println(ci.getName()) ;
106 if (!(ci.getName().compareTo("bundle") == 0)) {
107 Map properties = ci.getProperties();
108 for (Iterator k = properties.keySet().iterator(); k.hasNext();) {
109 PElement p = new PElement();
110 String key = (String) k.next();
111 List values = (List) properties.get(key);
112 for (Iterator v = values.iterator(); v.hasNext();) {
113 Object value = v.next();
114 p.setN(key);
115 if (value != null) {
116 p.setV(value.toString());
117 } else {
118 System.out.println("Missing value " + key);
119 }
120 String type = null;
121 if (value instanceof Number) {
122 type = "number";
123 } else {
124 if (value.getClass() == VersionImpl.class) { type = "version"; }
125 }
126 if (type != null) {
127 p.setT(type);
128 }
129 }
130 capability.addP(p);
131 }
132
133 list.add(capability);
134 }
135 }
136 return list;
137 }
138
139 /**
140 * extract requirement from bindex information.
141 * @return bundle requirement List
142 */
143 public List getRequirement() {
144 List list = new ArrayList();
145 Collection res = m_resource.getRequirementList();
146 Iterator it = res.iterator();
147 while (it.hasNext()) {
148 RequirementImpl ci = (RequirementImpl) it.next();
149 Require require = new Require();
150
151 require.setExtend(String.valueOf(ci.isExtend()));
152 require.setMultiple(String.valueOf(ci.isMultiple()));
153 require.setOptional(String.valueOf(ci.isOptional()));
154 require.setName(ci.getName());
155 require.setFilter(this.parseFilter(ci.getFilter()));
156 require.setValue(ci.getComment());
157 list.add(require);
158 }
159 return list;
160 }
161
162 /**
163 * extract symbolic name from bindex information.
164 * @return bundle symbolic name
165 */
166 public String getSymbolicName() {
167 return m_resource.getSymbolicName();
168 }
169
170 /**
171 * extract version from bindex information.
172 * @return bundle version
173 */
174 public String getVersion() {
175 if (m_resource.getVersion() != null) {
176 return m_resource.getVersion().toString();
177 } else {
178 return null;
179 }
180 }
181
182 /**
183 * extract presentation name from bindex information.
184 * @return bundle presentation name
185 */
186 public String getPresentationName() {
187 return m_resource.getPresentationName();
188 }
189
190 /**
191 * extract copyright from bindex information.
192 * @return bundle copyright
193 */
194 public String getCopyright() {
195 return m_resource.getCopyright();
196 }
197
198 /**
199 * extract description from bindex information.
200 * @return bundle description
201 */
202 public String getDescription() {
203 return m_resource.getDescription();
204 }
205
206 /**
207 * extract documentation from bindex information.
208 * @return bundle documentation
209 */
210 public String getDocumentation() {
211 if (m_resource.getDocumentation() != null) {
212 return m_resource.getDocumentation().toString();
213 } else {
214 return null;
215 }
216 }
217
218 /**
219 * extract license from bindex information.
220 * @return bundle license
221 */
222 public String getLicense() {
223 if (m_resource.getLicense() != null) {
224 return m_resource.getLicense().toString();
225 } else {
226 return null;
227 }
228 }
229
230 /**
231 * extract source from bindex information.
232 * @return bundle source
233 */
234 public String getSource() {
235 if (m_resource.getSource() != null) {
236 return m_resource.getSource().toString();
237 } else {
238 return null;
239 }
240 }
241
242}