Stefano Lenzi | 476013d | 2007-09-21 23:59:54 +0000 | [diff] [blame] | 1 | /*
|
| 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 | */
|
| 19 | package org.apache.felix.sandbox.obr.plugin;
|
| 20 |
|
| 21 | import java.util.ArrayList;
|
| 22 | import java.util.Iterator;
|
| 23 | import java.util.List;
|
| 24 |
|
| 25 | import org.apache.maven.plugin.logging.Log;
|
| 26 | import org.apache.maven.project.MavenProject;
|
| 27 | import org.w3c.dom.Document;
|
| 28 | import org.w3c.dom.Element;
|
| 29 | import org.w3c.dom.Node;
|
| 30 |
|
| 31 | /**
|
| 32 | * this class describe all information by bundle.
|
| 33 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
|
| 34 | */
|
| 35 | public class ResourcesBundle {
|
| 36 | /**
|
| 37 | * store the bundle symbolic name.
|
| 38 | */
|
| 39 | private String m_symbolicName;
|
| 40 |
|
| 41 | /**
|
| 42 | * store the bundle presentation name.
|
| 43 | */
|
| 44 | private String m_presentationName;
|
| 45 |
|
| 46 | /**
|
| 47 | * store the bundle version.
|
| 48 | */
|
| 49 | private String m_version;
|
| 50 |
|
| 51 | /**
|
| 52 | * store the bundle URI.
|
| 53 | */
|
| 54 | private String m_uri;
|
| 55 |
|
| 56 | /**
|
| 57 | * store the bundle description.
|
| 58 | */
|
| 59 | private String m_description;
|
| 60 |
|
| 61 | /**
|
| 62 | * store the bundle size.
|
| 63 | */
|
| 64 | private String m_size;
|
| 65 |
|
| 66 | /**
|
| 67 | * store the bundle documentation.
|
| 68 | */
|
| 69 | private String m_documentation;
|
| 70 |
|
| 71 | /**
|
| 72 | * store the bundle source.
|
| 73 | */
|
| 74 | private String m_source;
|
| 75 |
|
| 76 | /**
|
| 77 | * store the bundle license.
|
| 78 | */
|
| 79 | private String m_license;
|
| 80 |
|
| 81 | /**
|
| 82 | * store the bundle id.
|
| 83 | */
|
| 84 | private String m_id;
|
| 85 |
|
| 86 | /**
|
| 87 | * store the bundle categories.
|
| 88 | */
|
| 89 | private List m_category = new ArrayList();
|
| 90 |
|
| 91 | /**
|
| 92 | * store the bundle capabilities.
|
| 93 | */
|
| 94 | private List m_capability = new ArrayList();
|
| 95 |
|
| 96 | /**
|
| 97 | * store the bundle requirement.
|
| 98 | */
|
| 99 | private List m_require = new ArrayList();
|
| 100 |
|
| 101 | /**
|
| 102 | * get the plugin logger.
|
| 103 | */
|
| 104 | private Log m_logger;
|
| 105 |
|
| 106 | /**
|
| 107 | * initialize logger.
|
| 108 | * @param log log use by plugin
|
| 109 | */
|
| 110 | public ResourcesBundle(Log log) {
|
| 111 | m_logger = log;
|
| 112 | }
|
| 113 |
|
| 114 | public List getCapability() {
|
| 115 | return m_capability;
|
| 116 | }
|
| 117 |
|
| 118 | public void setCapability(List capability) {
|
| 119 | this.m_capability = capability;
|
| 120 | }
|
| 121 |
|
| 122 | public List getCategory() {
|
| 123 | return m_category;
|
| 124 | }
|
| 125 |
|
| 126 | public void setCategory(List category) {
|
| 127 | this.m_category = category;
|
| 128 | }
|
| 129 |
|
| 130 | public String getLicense() {
|
| 131 | return m_license;
|
| 132 | }
|
| 133 |
|
| 134 | public void setLicense(String license) {
|
| 135 | this.m_license = license;
|
| 136 | }
|
| 137 |
|
| 138 | public String getDescription() {
|
| 139 | return m_description;
|
| 140 | }
|
| 141 |
|
| 142 | public void setDescription(String description) {
|
| 143 | this.m_description = description;
|
| 144 | }
|
| 145 |
|
| 146 | public String getDocumentation() {
|
| 147 | return m_documentation;
|
| 148 | }
|
| 149 |
|
| 150 | public void setDocumentation(String documentation) {
|
| 151 | this.m_documentation = documentation;
|
| 152 | }
|
| 153 |
|
| 154 | public String getPresentationName() {
|
| 155 | return m_presentationName;
|
| 156 | }
|
| 157 |
|
| 158 | public void setPresentationName(String name) {
|
| 159 | m_presentationName = name;
|
| 160 | }
|
| 161 |
|
| 162 | public String getSize() {
|
| 163 | return m_size;
|
| 164 | }
|
| 165 |
|
| 166 | public void setSize(String size) {
|
| 167 | this.m_size = size;
|
| 168 | }
|
| 169 |
|
| 170 | public String getSymbolicName() {
|
| 171 | return m_symbolicName;
|
| 172 | }
|
| 173 |
|
| 174 | public void setSymbolicName(String name) {
|
| 175 | m_symbolicName = name;
|
| 176 | }
|
| 177 |
|
| 178 | public String getUri() {
|
| 179 | return m_uri;
|
| 180 | }
|
| 181 |
|
| 182 | public void setUri(String url) {
|
| 183 | this.m_uri = url;
|
| 184 | }
|
| 185 |
|
| 186 | public String getVersion() {
|
| 187 | return m_version;
|
| 188 | }
|
| 189 |
|
| 190 | public void setVersion(String version) {
|
| 191 | this.m_version = version;
|
| 192 | }
|
| 193 |
|
| 194 | public List getRequire() {
|
| 195 | return m_require;
|
| 196 | }
|
| 197 |
|
| 198 | public void setRequire(List require) {
|
| 199 | this.m_require = require;
|
| 200 | }
|
| 201 |
|
| 202 | public String getSource() {
|
| 203 | return m_source;
|
| 204 | }
|
| 205 |
|
| 206 | public void setSource(String source) {
|
| 207 | this.m_source = source;
|
| 208 | }
|
| 209 |
|
| 210 | public String getId() {
|
| 211 | return m_id;
|
| 212 | }
|
| 213 |
|
| 214 | public void setId(String id) {
|
| 215 | this.m_id = id;
|
| 216 | }
|
| 217 |
|
| 218 | /**
|
| 219 | * add a new capability for this bundle description.
|
| 220 | * @param capability the Capability to add
|
| 221 | */
|
| 222 | public void addCapability(Capability capability) {
|
| 223 | m_capability.add(capability);
|
| 224 | }
|
| 225 |
|
| 226 | /**
|
| 227 | * add a new requirement for this bundle description.
|
| 228 | * @param require th Require to add
|
| 229 | */
|
| 230 | public void addRequire(Require require) {
|
| 231 | m_require.add(require);
|
| 232 | }
|
| 233 |
|
| 234 | /**
|
| 235 | * add a new category for this bundle decription.
|
| 236 | * @param category the Category to add
|
| 237 | */
|
| 238 | public void addCategory(Category category) {
|
| 239 | m_category.add(category);
|
| 240 | }
|
| 241 |
|
| 242 | /**
|
| 243 | * transform this object to Node.
|
| 244 | * tranform all sub-object to node also
|
| 245 | * @param father father document for create Node
|
| 246 | * @return node
|
| 247 | */
|
| 248 | public Node getNode(Document father) {
|
| 249 | // return the complete resource tree
|
| 250 | if (!this.isValid() || this.getId() == null) {
|
| 251 | m_logger.error("those properties was not defined:" + this.getInvalidProperties());
|
| 252 | return null;
|
| 253 | }
|
| 254 |
|
| 255 | Element resource = father.createElement("resource");
|
| 256 | Element description = father.createElement("description");
|
| 257 | Element size = father.createElement("size");
|
| 258 | Element documentation = father.createElement("documentation");
|
| 259 | Element source = father.createElement("source");
|
| 260 | Element license = father.createElement("license");
|
| 261 |
|
| 262 | resource.setAttribute("id", this.getId());
|
| 263 | resource.setAttribute("symbolicname", this.getSymbolicName());
|
| 264 | resource.setAttribute("presentationname", this.getPresentationName());
|
| 265 | resource.setAttribute("uri", this.getUri());
|
| 266 | resource.setAttribute("version", this.getVersion());
|
| 267 |
|
Stuart McCulloch | bfc05cf | 2007-09-26 15:34:00 +0000 | [diff] [blame^] | 268 | XmlHelper.setTextContent(description,this.getDescription());
|
Stefano Lenzi | 476013d | 2007-09-21 23:59:54 +0000 | [diff] [blame] | 269 | resource.appendChild(description);
|
| 270 |
|
Stuart McCulloch | bfc05cf | 2007-09-26 15:34:00 +0000 | [diff] [blame^] | 271 | XmlHelper.setTextContent(size,this.getSize());
|
Stefano Lenzi | 476013d | 2007-09-21 23:59:54 +0000 | [diff] [blame] | 272 | resource.appendChild(size);
|
| 273 |
|
| 274 | if (this.getDocumentation() != null) {
|
Stuart McCulloch | bfc05cf | 2007-09-26 15:34:00 +0000 | [diff] [blame^] | 275 | XmlHelper.setTextContent(documentation,this.getDocumentation());
|
Stefano Lenzi | 476013d | 2007-09-21 23:59:54 +0000 | [diff] [blame] | 276 | resource.appendChild(documentation);
|
| 277 | }
|
| 278 |
|
| 279 | if (this.getSource() != null) {
|
Stuart McCulloch | bfc05cf | 2007-09-26 15:34:00 +0000 | [diff] [blame^] | 280 | XmlHelper.setTextContent(source,this.getSource());
|
Stefano Lenzi | 476013d | 2007-09-21 23:59:54 +0000 | [diff] [blame] | 281 | resource.appendChild(source);
|
| 282 | }
|
| 283 |
|
| 284 | if (this.getLicense() != null) {
|
Stuart McCulloch | bfc05cf | 2007-09-26 15:34:00 +0000 | [diff] [blame^] | 285 | XmlHelper.setTextContent(license, this.getLicense());
|
Stefano Lenzi | 476013d | 2007-09-21 23:59:54 +0000 | [diff] [blame] | 286 | resource.appendChild(license);
|
| 287 | }
|
| 288 |
|
| 289 | List list = (ArrayList) this.getNodeCategories(father);
|
| 290 | for (int i = 0; i < list.size(); i++) {
|
| 291 | resource.appendChild((Node) list.get(i));
|
| 292 | }
|
| 293 |
|
| 294 | list = (ArrayList) this.getNodeCapabilities(father);
|
| 295 | for (int i = 0; i < list.size(); i++) {
|
| 296 | resource.appendChild((Node) list.get(i));
|
| 297 | }
|
| 298 |
|
| 299 | list = (ArrayList) this.getNodeRequirement(father);
|
| 300 | for (int i = 0; i < list.size(); i++) {
|
| 301 | resource.appendChild((Node) list.get(i));
|
| 302 | }
|
| 303 |
|
| 304 | return resource;
|
| 305 | }
|
| 306 |
|
| 307 | /**
|
| 308 | * this method gets information form pom.xml to complete missing data from those given by user.
|
| 309 | * @param project project information given by maven
|
| 310 | * @param ebi bundle information extracted from bindex
|
| 311 | * @return true
|
| 312 | */
|
| 313 | public boolean construct(MavenProject project, ExtractBindexInfo ebi) {
|
| 314 |
|
| 315 | if (ebi.getPresentationName() != null) {
|
| 316 | this.setPresentationName(ebi.getPresentationName());
|
| 317 | if (project.getName() != null) { m_logger.warn("pom property override:<presentationname> " + project.getName()); }
|
| 318 | } else {
|
| 319 | this.setPresentationName(project.getName());
|
| 320 | }
|
| 321 |
|
| 322 | if (ebi.getSymbolicName() != null) {
|
| 323 | this.setSymbolicName(ebi.getSymbolicName());
|
| 324 | if (project.getArtifactId() != null) { m_logger.warn("pom property override:<symbolicname> " + project.getArtifactId()); }
|
| 325 | } else {
|
| 326 | this.setSymbolicName(project.getArtifactId());
|
| 327 | }
|
| 328 |
|
| 329 | if (ebi.getVersion() != null) {
|
| 330 | this.setVersion(ebi.getVersion());
|
| 331 | if (project.getVersion() != null) { m_logger.warn("pom property override:<version> " + project.getVersion()); }
|
| 332 | } else {
|
| 333 | this.setVersion(project.getVersion());
|
| 334 | }
|
| 335 |
|
| 336 | if (ebi.getDescription() != null) {
|
| 337 | this.setDescription(ebi.getDescription());
|
| 338 | if (project.getDescription() != null) { m_logger.warn("pom property override:<description> " + project.getDescription()); }
|
| 339 | } else {
|
| 340 | this.setDescription(project.getDescription());
|
| 341 | }
|
| 342 |
|
| 343 | if (ebi.getDocumentation() != null) {
|
| 344 | this.setDocumentation(ebi.getDocumentation());
|
| 345 | if (project.getUrl() != null) { m_logger.warn("pom property override:<documentation> " + project.getUrl()); }
|
| 346 | } else {
|
| 347 | this.setDocumentation(project.getUrl());
|
| 348 | }
|
| 349 |
|
| 350 | if (ebi.getSource() != null) {
|
| 351 | this.setSource(ebi.getSource());
|
| 352 | if (project.getScm() != null) { m_logger.warn("pom property override:<source> " + project.getScm()); }
|
| 353 | } else {
|
| 354 | String src = null;
|
| 355 | if (project.getScm() != null) { src = project.getScm().getUrl(); }
|
| 356 | this.setSource(src);
|
| 357 | }
|
| 358 |
|
| 359 | if (ebi.getLicense() != null) {
|
| 360 | this.setLicense(ebi.getLicense());
|
| 361 | String lic = null;
|
| 362 | List l = project.getLicenses();
|
| 363 | Iterator it = l.iterator();
|
| 364 | while (it.hasNext()) {
|
| 365 | if (it.next() != null) {
|
| 366 | m_logger.warn("pom property override:<source> " + lic);
|
| 367 | break;
|
| 368 | }
|
| 369 | }
|
| 370 | } else {
|
| 371 | String lic = null;
|
| 372 | List l = project.getLicenses();
|
| 373 | Iterator it = l.iterator();
|
| 374 | while (it.hasNext()) {
|
| 375 | lic = it.next() + ";";
|
| 376 | }
|
| 377 |
|
| 378 | this.setLicense(lic);
|
| 379 | }
|
| 380 |
|
| 381 | // create the first capability (ie : bundle)
|
| 382 | Capability capability = new Capability();
|
| 383 | capability.setName("bundle");
|
| 384 | PElement p = new PElement();
|
| 385 | p.setN("manifestversion");
|
| 386 | p.setV("2");
|
| 387 | capability.addP(p);
|
| 388 |
|
| 389 | p = new PElement();
|
| 390 | p.setN("presentationname");
|
| 391 | p.setV(this.getPresentationName());
|
| 392 | capability.addP(p);
|
| 393 |
|
| 394 | p = new PElement();
|
| 395 | p.setN("symbolicname");
|
| 396 | p.setV(this.getSymbolicName());
|
| 397 | capability.addP(p);
|
| 398 |
|
| 399 | p = new PElement();
|
| 400 | p.setN("version");
|
| 401 | p.setT("version");
|
| 402 | p.setV(this.getVersion());
|
| 403 | capability.addP(p);
|
| 404 |
|
| 405 | this.addCapability(capability);
|
| 406 |
|
| 407 | List capabilities = (ArrayList) ebi.getCapabilities();
|
| 408 | for (int i = 0; i < capabilities.size(); i++) {
|
| 409 | this.addCapability((Capability) capabilities.get(i));
|
| 410 | }
|
| 411 |
|
| 412 | List requirement = (ArrayList) ebi.getRequirement();
|
| 413 | for (int i = 0; i < requirement.size(); i++) {
|
| 414 | this.addRequire((Require) requirement.get(i));
|
| 415 | }
|
| 416 |
|
| 417 | // we also add the goupId
|
| 418 | Category category = new Category();
|
| 419 | category.setId(project.getGroupId());
|
| 420 | this.addCategory(category);
|
| 421 |
|
| 422 | return true;
|
| 423 | }
|
| 424 |
|
| 425 | /**
|
| 426 | * return if the bundle resource is complete.
|
| 427 | * @return false if an information is missing, else true
|
| 428 | */
|
| 429 | public boolean isValid() {
|
| 430 | // we must verify require properties are present
|
| 431 | return this.getPresentationName() != null
|
| 432 | && this.getSymbolicName() != null
|
| 433 | && this.getVersion() != null
|
| 434 | && this.getUri() != null
|
| 435 | && this.getSize() != null;
|
| 436 | }
|
| 437 |
|
| 438 | /**
|
| 439 | * test if this bundle has the same symbolicname, presentationname and version number.
|
| 440 | * @param symbolicName symbolicName to compare with current bundle
|
| 441 | * @param presentationName presentationName to compare with current bundlde
|
| 442 | * @param version version to compare with current bundle
|
| 443 | * @return true if the information are the same, else false
|
| 444 | */
|
| 445 | public boolean isSameBundleResource(String symbolicName, String presentationName, String version) {
|
| 446 | if (this.isValid()) {
|
| 447 | boolean result;
|
| 448 | result = (symbolicName.compareTo(this.getSymbolicName()) == 0) && (version.compareTo(this.getVersion()) == 0) && (presentationName.compareTo(this.getPresentationName()) == 0);
|
| 449 | return result;
|
| 450 |
|
| 451 | } else {
|
| 452 | return false;
|
| 453 | }
|
| 454 |
|
| 455 | }
|
| 456 |
|
| 457 | /**
|
| 458 | * return a list of categories transformed to node.
|
| 459 | * @param father father document to create node from same document
|
| 460 | * @return List of Node
|
| 461 | */
|
| 462 | private List getNodeCategories(Document father) {
|
| 463 | List listNode = new ArrayList();
|
| 464 | List listCategory = (ArrayList) this.getCategory();
|
| 465 | for (int i = 0; i < listCategory.size(); i++) {
|
| 466 | listNode.add(((Category) listCategory.get(i)).getNode(father));
|
| 467 | }
|
| 468 | return listNode;
|
| 469 | }
|
| 470 |
|
| 471 | /**
|
| 472 | * return a list of capabilities transformed to node.
|
| 473 | * @param father father document to create node from same document
|
| 474 | * @return List of Node
|
| 475 | */
|
| 476 | private List getNodeCapabilities(Document father) {
|
| 477 | List listNode = new ArrayList();
|
| 478 | List listCapability = (ArrayList) this.getCapability();
|
| 479 | for (int i = 0; i < listCapability.size(); i++) {
|
| 480 | listNode.add(((Capability) listCapability.get(i)).getNode(father));
|
| 481 | }
|
| 482 | return listNode;
|
| 483 | }
|
| 484 |
|
| 485 | /**
|
| 486 | * return a list of requirement transformed to node.
|
| 487 | * @param father father document to create node from same document
|
| 488 | * @return List of Node.
|
| 489 | */
|
| 490 | private List getNodeRequirement(Document father) {
|
| 491 | List listNode = new ArrayList();
|
| 492 | List listRequirement = (ArrayList) this.getRequire();
|
| 493 | for (int i = 0; i < listRequirement.size(); i++) {
|
| 494 | listNode.add(((Require) listRequirement.get(i)).getNode(father));
|
| 495 | }
|
| 496 | return listNode;
|
| 497 | }
|
| 498 |
|
| 499 | /**
|
| 500 | * return the list of properties not define in this bundle resource.
|
| 501 | * @return list of properties not define
|
| 502 | */
|
| 503 | private String getInvalidProperties() {
|
| 504 | if (this.isValid()) {
|
| 505 | if (this.getId() == null) {
|
| 506 | return "id";
|
| 507 | } else {
|
| 508 | return "";
|
| 509 | }
|
| 510 | }
|
| 511 | String result = "";
|
| 512 | if (this.getPresentationName() == null) { result = result + "presentationName;"; }
|
| 513 | if (this.getSymbolicName() == null) { result = result + "symbolicName;"; }
|
| 514 | if (this.getVersion() == null) { result = result + "version;"; }
|
| 515 | if (this.getUri() == null) { result = result + "Uri;"; }
|
| 516 | if (this.getSize() == null) { result = result + "Size"; }
|
| 517 | return result;
|
| 518 | }
|
| 519 |
|
| 520 | }
|