This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.0.4-incubator in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git
commit d40ec925e2def59b3674b0f78f995bb5580fa36f Author: Felix Meschberger <[email protected]> AuthorDate: Thu Jun 18 09:21:02 2009 +0000 Move Sling to new TLP location git-svn-id: https://svn.eu.apache.org/repos/asf/sling/tags/org.apache.sling.commons.osgi-2.0.4-incubator@785979 13f79535-47bb-0310-9956-ffa450edef68 --- LICENSE | 202 ++++++++++++ NOTICE | 8 + README.txt | 37 +++ pom.xml | 77 +++++ .../apache/sling/commons/osgi/ManifestHeader.java | 344 +++++++++++++++++++++ .../org/apache/sling/commons/osgi/OsgiUtil.java | 265 ++++++++++++++++ src/main/resources/META-INF/DISCLAIMER | 7 + src/main/resources/META-INF/LICENSE | 202 ++++++++++++ src/main/resources/META-INF/NOTICE | 9 + .../sling/commons/osgi/ManifestHeaderTest.java | 136 ++++++++ 10 files changed, 1287 insertions(+) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..5e1fc1f --- /dev/null +++ b/NOTICE @@ -0,0 +1,8 @@ +Apache Sling Commons OSGi +Copyright 2008-2009 The Apache Software Foundation + +Apache Sling is based on source code originally developed +by Day Software (http://www.day.com/). + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..5ce8754 --- /dev/null +++ b/README.txt @@ -0,0 +1,37 @@ +Apache Sling Commons OSGi + +The Commons OSGi library provides utility methods when implementing OSGi related services +and components. + +Disclaimer +========== +Apache Sling is an effort undergoing incubation at The Apache Software Foundation (ASF), +sponsored by the Apache Jackrabbit PMC. Incubation is required of all newly accepted +projects until a further review indicates that the infrastructure, communications, +and decision making process have stabilized in a manner consistent with other +successful ASF projects. While incubation status is not necessarily a reflection of +the completeness or stability of the code, it does indicate that the project has yet +to be fully endorsed by the ASF. + +Getting Started +=============== + +This component uses a Maven 2 (http://maven.apache.org/) build +environment. It requires a Java 5 JDK (or higher) and Maven (http://maven.apache.org/) +2.0.7 or later. We recommend to use the latest Maven version. + +If you have Maven 2 installed, you can compile and +package the jar using the following command: + + mvn package + +See the Maven 2 documentation for other build features. + +The latest source code for this component is available in the +Subversion (http://subversion.tigris.org/) source repository of +the Apache Software Foundation. If you have Subversion installed, +you can checkout the latest source using the following command: + + svn checkout http://svn.apache.org/repos/asf/incubator/sling/trunk/commons/osgi + +See the Subversion documentation for other source control features. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..af6424c --- /dev/null +++ b/pom.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.sling</groupId> + <artifactId>sling</artifactId> + <version>5-incubator</version> + <relativePath>../../../parent/pom.xml</relativePath> + </parent> + + <artifactId>org.apache.sling.commons.osgi</artifactId> + <version>2.0.4-incubator</version> + <packaging>bundle</packaging> + + <name>Apache Sling Commons OSGi support</name> + <description>Commons OSGi</description> + + <scm> + <connection>scm:svn:http://svn.apache.org/repos/asf/incubator/sling/tags/org.apache.sling.commons.osgi-2.0.4-incubator</connection> + <developerConnection>scm:svn:https://svn.apache.org/repos/asf/incubator/sling/tags/org.apache.sling.commons.osgi-2.0.4-incubator</developerConnection> + <url>http://svn.apache.org/viewvc/incubator/sling/tags/org.apache.sling.commons.osgi-2.0.4-incubator</url> + </scm> + + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <extensions>true</extensions> + <configuration> + <instructions> + <Export-Package> + org.apache.sling.commons.osgi;version=${pom.version} + </Export-Package> + </instructions> + </configuration> + </plugin> + </plugins> + </build> + + <dependencies> + <!-- OSGi Libraries --> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.osgi.compendium</artifactId> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </dependency> + </dependencies> + +</project> diff --git a/src/main/java/org/apache/sling/commons/osgi/ManifestHeader.java b/src/main/java/org/apache/sling/commons/osgi/ManifestHeader.java new file mode 100644 index 0000000..72c951b --- /dev/null +++ b/src/main/java/org/apache/sling/commons/osgi/ManifestHeader.java @@ -0,0 +1,344 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.commons.osgi; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * This is a helper class to parse manifest header entries. + */ +public class ManifestHeader { + + /** + * A header can have several entries separated by comma. + */ + public interface Entry { + + /** + * The value of the entry. + */ + String getValue(); + + /** + * The attributes specified for this entry. + */ + NameValuePair[] getAttributes(); + + /** + * The directives for this entry. + */ + NameValuePair[] getDirectives(); + + String getAttributeValue(String name); + + String getDirectiveValue(String name); +} + + /** The entries for this header. */ + private Entry[] entries = new Entry[0]; + + /** + * Add new entries from parsing. + */ + private void add(Entry[] paths) { + if ( paths != null && paths.length > 0 ) { + final Entry[] copy = new Entry[this.entries.length + paths.length]; + System.arraycopy(this.entries, 0, copy, 0, this.entries.length); + System.arraycopy(paths, 0, copy, this.entries.length, paths.length); + this.entries = copy; + } + } + + /** + * Return the entries for this header. + */ + public Entry[] getEntries() { + return this.entries; + } + + /** + * Directives and attributes are simple name/value pairs. + */ + public final static class NameValuePair { + + private final String name; + private final String value; + + public NameValuePair(String name, String value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public String getValue() { + return value; + } + } + + private static final String CLASS_PATH_SEPARATOR = ","; + private static final String PACKAGE_SEPARATOR = ";"; + private static final String DIRECTIVE_SEPARATOR = ":="; + private static final String ATTRIBUTE_SEPARATOR = "="; + + /** + * Parse headers + * Like this: path; path; dir1:=dirval1; dir2:=dirval2; attr1=attrval1; attr2=attrval2, + * path; path; dir1:=dirval1; dir2:=dirval2; attr1=attrval1; attr2=attrval2 + * The returned object maintains the order of entries (paths), directives and attributes. + */ + public static ManifestHeader parse(String header) { + final ManifestHeader entry = new ManifestHeader(); + + if (header != null) { + if (header.length() == 0) { + throw new IllegalArgumentException("A header cannot be an empty string."); + } + + final String[] clauseStrings = parseDelimitedString(header, CLASS_PATH_SEPARATOR); + if ( clauseStrings != null ) { + for(final String clause : clauseStrings) { + entry.add(parseStandardHeaderClause(clause)); + } + } + } + + return (entry.getEntries().length == 0) ? null : entry; + } + + /** + * Parse a clause + * Like this: path; path; dir1:=dirval1; dir2:=dirval2; attr1=attrval1; attr2=attrval2 + */ + private static ManifestHeader.Entry[] parseStandardHeaderClause(String clauseString) + throws IllegalArgumentException { + // Break string into semi-colon delimited pieces. + String[] pieces = parseDelimitedString(clauseString, PACKAGE_SEPARATOR); + + // Count the number of different paths; paths + // will not have an '=' in their string. This assumes + // that paths come first, before directives and + // attributes. + int pathCount = 0; + for (int pieceIdx = 0; pieceIdx < pieces.length; pieceIdx++) { + if (pieces[pieceIdx].indexOf('=') >= 0) { + break; + } + pathCount++; + } + + // Error if no paths were specified. + if (pathCount == 0) { + throw new IllegalArgumentException( + "No paths specified in header: " + clauseString); + } + + // Create an array of paths. + PathImpl[] paths = new PathImpl[pathCount]; + for(int i=0;i<pathCount;i++) { + paths[i] = new PathImpl(pieces[i]); + } + + // Parse the directives/attributes + // and keep the order + // for simpliefied checking if a directive/attribute is used twice, we keep + // two collections: one for the values and one for the names + final List<ManifestHeader.NameValuePair> dirsList = new ArrayList<ManifestHeader.NameValuePair>(); + final Set<String> dirsNames = new HashSet<String>(); + final List<ManifestHeader.NameValuePair> attrsList = new ArrayList<ManifestHeader.NameValuePair>(); + final Set<String> attrsNames = new HashSet<String>(); + + int idx = -1; + String sep = null; + for (int pieceIdx = pathCount; pieceIdx < pieces.length; pieceIdx++) { + + if ((idx = pieces[pieceIdx].indexOf(DIRECTIVE_SEPARATOR)) >= 0) { + sep = DIRECTIVE_SEPARATOR; + } else if ((idx = pieces[pieceIdx].indexOf(ATTRIBUTE_SEPARATOR)) >= 0) { + sep = ATTRIBUTE_SEPARATOR; + } else { + throw new IllegalArgumentException("Not a directive/attribute: " + clauseString); + } + + final String key = pieces[pieceIdx].substring(0, idx).trim(); + String value = pieces[pieceIdx].substring(idx + sep.length()).trim(); + + // Remove quotes, if value is quoted. + if (value.startsWith("\"") && value.endsWith("\"")) { + value = value.substring(1, value.length() - 1); + } + + // Save the directive/attribute in the appropriate array. + if (sep.equals(DIRECTIVE_SEPARATOR)) { + // Check for duplicates. + if (dirsNames.contains(key)) { + throw new IllegalArgumentException("Duplicate directive: " + key); + } + dirsList.add(new ManifestHeader.NameValuePair(key, value)); + dirsNames.add(key); + } else { + // Check for duplicates. + if (attrsNames.contains(key)) { + throw new IllegalArgumentException("Duplicate attribute: " + key); + } + attrsList.add(new ManifestHeader.NameValuePair(key, value)); + attrsNames.add(key); + } + } + // Create directive array. + ManifestHeader.NameValuePair[] dirs = + dirsList.toArray(new ManifestHeader.NameValuePair[dirsList.size()]); + + // Create attribute array. + ManifestHeader.NameValuePair[] attrs = + attrsList.toArray(new ManifestHeader.NameValuePair[attrsList.size()]); + + // now set attributes and directives for each path + for(int i=0;i<pathCount;i++) { + paths[i].init(dirs, attrs); + } + + return paths; + } + + private static final int CHAR = 1; + private static final int DELIMITER = 2; + private static final int STARTQUOTE = 4; + private static final int ENDQUOTE = 8; + + /** + * Parses delimited string and returns an array containing the tokens. This + * parser obeys quotes, so the delimiter character will be ignored if it is + * inside of a quote. This method assumes that the quote character is not + * included in the set of delimiter characters. + * @param value the delimited string to parse. + * @param delim the characters delimiting the tokens. + * @return an array of string tokens or null if there were no tokens. + **/ + private static String[] parseDelimitedString(String value, String delim) { + if (value == null) { + value = ""; + } + + final List<String> list = new ArrayList<String>(); + + final StringBuffer sb = new StringBuffer(); + + int expecting = (CHAR | DELIMITER | STARTQUOTE); + + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + + boolean isDelimiter = (delim.indexOf(c) >= 0); + boolean isQuote = (c == '"'); + + if (isDelimiter && ((expecting & DELIMITER) > 0)) { + list.add(sb.toString().trim()); + sb.delete(0, sb.length()); + expecting = (CHAR | DELIMITER | STARTQUOTE); + } else if (isQuote && ((expecting & STARTQUOTE) > 0)) { + sb.append(c); + expecting = CHAR | ENDQUOTE; + } else if (isQuote && ((expecting & ENDQUOTE) > 0)) { + sb.append(c); + expecting = (CHAR | STARTQUOTE | DELIMITER); + } else if ((expecting & CHAR) > 0) { + sb.append(c); + } else { + throw new IllegalArgumentException("Invalid delimited string: " + value); + } + } + + if (sb.length() > 0) { + list.add(sb.toString().trim()); + } + + if ( list.size() == 0 ) { + return null; + } + return list.toArray(new String[list.size()]); + } + + protected static final class PathImpl implements ManifestHeader.Entry { + + private final String value; + + private NameValuePair[] attributes; + private NameValuePair[] directives; + + public PathImpl(final String path) { + this.value = path; + } + + public void init(NameValuePair[] dirs, NameValuePair[] attrs) { + this.directives = dirs; + this.attributes = attrs; + } + + /** + * @see org.apache.sling.commons.osgi.ManifestHeader.Entry#getAttributes() + */ + public NameValuePair[] getAttributes() { + return this.attributes; + } + + /** + * @see org.apache.sling.commons.osgi.ManifestHeader.Entry#getDirectives() + */ + public NameValuePair[] getDirectives() { + return this.directives; + } + + /** + * @see org.apache.sling.commons.osgi.ManifestHeader.Entry#getValue() + */ + public String getValue() { + return this.value; + } + + public String getAttributeValue(String name) { + String v = null; + int index = 0; + while ( v == null && index < attributes.length ) { + if ( attributes[index].getName().equals(name) ) { + v = attributes[index].getValue(); + } + index++; + } + return v; + } + + public String getDirectiveValue(String name) { + String v = null; + int index = 0; + while ( v == null && index < directives.length ) { + if ( directives[index].getName().equals(name) ) { + v = directives[index].getValue(); + } + index++; + } + return v; + } + } +} diff --git a/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java new file mode 100644 index 0000000..7c02c03 --- /dev/null +++ b/src/main/java/org/apache/sling/commons/osgi/OsgiUtil.java @@ -0,0 +1,265 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.commons.osgi; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.List; +import java.util.Map; + +import org.osgi.framework.Bundle; +import org.osgi.framework.ServiceReference; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventConstants; + +/** + * The <code>OsgiUtil</code> is a utility class providing some usefull utility + * methods. + */ +public class OsgiUtil { + + /** + * Returns the boolean value of the named service reference property or the + * <code>defaultValue</code> if no such service reference property exist. + * If the service property is not a <code>Boolean</code> it is converted + * by calling <code>Boolean.valueOf</code> on the string value of the + * property. + */ + public static boolean toBoolean(Object propValue, boolean defaultValue) { + propValue = toObject(propValue); + if (propValue instanceof Boolean) { + return (Boolean) propValue; + } else if (propValue != null) { + return Boolean.valueOf(String.valueOf(propValue)); + } + + return defaultValue; + } + + /** + * Returns the named service reference property as a string or the + * <code>defaultValue</code> if no such reference property exists. + */ + public static String toString(Object propValue, String defaultValue) { + propValue = toObject(propValue); + return (propValue != null) ? propValue.toString() : defaultValue; + } + + /** + * Returns the named service reference property as a long or the + * <code>defaultValue</code> if no such reference property exists or if + * the property is not an <code>Integer</code> and cannot be converted to + * a <code>Long</code> from the property's string value. + */ + public static long toLong(Object propValue, long defaultValue) { + propValue = toObject(propValue); + if (propValue instanceof Long) { + return (Long) propValue; + } else if (propValue != null) { + try { + return Long.valueOf(String.valueOf(propValue)); + } catch (NumberFormatException nfe) { + // don't care, fall through to default value + } + } + + return defaultValue; + } + + /** + * Returns the named service reference property as an integer or the + * <code>defaultValue</code> if no such reference property exists or if + * the property is not an <code>Integer</code> and cannot be converted to + * an <code>Integer</code> from the property's string value. + */ + public static int toInteger(Object propValue, int defaultValue) { + propValue = toObject(propValue); + if (propValue instanceof Integer) { + return (Integer) propValue; + } else if (propValue != null) { + try { + return Integer.valueOf(String.valueOf(propValue)); + } catch (NumberFormatException nfe) { + // don't care, fall through to default value + } + } + + return defaultValue; + } + + /** + * Returns the named service reference property as a double or the + * <code>defaultValue</code> if no such reference property exists or if + * the property is not an <code>Double</code> and cannot be converted to + * an <code>Double</code> from the property's string value. + * + * @deprecated since 2.0.4, use {@link #toDouble(Object, double)} instead + */ + @Deprecated + public static double getProperty(Object propValue, double defaultValue) { + return toDouble(propValue, defaultValue); + } + + /** + * Returns the named service reference property as a double or the + * <code>defaultValue</code> if no such reference property exists or if + * the property is not an <code>Double</code> and cannot be converted to + * an <code>Double</code> from the property's string value. + * + * @since 2.0.4 + */ + public static double toDouble(Object propValue, double defaultValue) { + propValue = toObject(propValue); + if (propValue instanceof Double) { + return (Double) propValue; + } else if (propValue != null) { + try { + return Double.valueOf(String.valueOf(propValue)); + } catch (NumberFormatException nfe) { + // don't care, fall through to default value + } + } + + return defaultValue; + } + + /** + * Returns the named service reference property as a single value. If the + * property is neither an array nor a <code>java.util.Collection</code> the + * property is returned unmodified. If the property is a non-empty array, + * the first array element is returned. If the property is a non-empty + * <code>java.util.Collection</code>, the first collection element is returned. + * Otherwise <code>null</code> is returned. + */ + public static Object toObject(Object propValue) { + if (propValue == null) { + return null; + } else if (propValue.getClass().isArray()) { + Object[] prop = (Object[]) propValue; + return prop.length > 0 ? prop[0] : null; + } else if (propValue instanceof Collection) { + Collection<?> prop = (Collection<?>) propValue; + return prop.isEmpty() ? null : prop.iterator().next(); + } else { + return propValue; + } + } + + /** + * Returns the named service reference property as an array of Strings. If + * the property is a scalar value its string value is returned as a single + * element array. If the property is an array, the elements are converted to + * String objects and returned as an array. If the property is a collection, the + * collection elements are converted to String objects and returned as an array. + * Otherwise (if the property does not exist) <code>null</code> is + * returned. + */ + public static String[] toStringArray(Object propValue) { + return toStringArray(propValue, null); + } + + /** + * Returns the named service reference property as an array of Strings. If + * the property is a scalar value its string value is returned as a single + * element array. If the property is an array, the elements are converted to + * String objects and returned as an array. If the property is a collection, the + * collection elements are converted to String objects and returned as an array. + * Otherwise (if the property does not exist) a provided default value is + * returned. + * @since 2.0.4 + */ + public static String[] toStringArray(Object propValue, String[] defaultArray) { + if (propValue == null) { + // no value at all + return defaultArray; + + } else if (propValue instanceof String) { + // single string + return new String[] { (String) propValue }; + + } else if (propValue instanceof String[]) { + // String[] + return (String[]) propValue; + + } else if (propValue.getClass().isArray()) { + // other array + Object[] valueArray = (Object[]) propValue; + List<String> values = new ArrayList<String>(valueArray.length); + for (Object value : valueArray) { + if (value != null) { + values.add(value.toString()); + } + } + return values.toArray(new String[values.size()]); + + } else if (propValue instanceof Collection) { + // collection + Collection<?> valueCollection = (Collection<?>) propValue; + List<String> valueList = new ArrayList<String>(valueCollection.size()); + for (Object value : valueCollection) { + if (value != null) { + valueList.add(value.toString()); + } + } + return valueList.toArray(new String[valueList.size()]); + } + + return defaultArray; + } + + public static Event createEvent(Bundle sourceBundle, + ServiceReference sourceService, String eventName, + Map<String, Object> props) { + + // get a private copy of the properties + Dictionary<String, Object> table = new Hashtable<String, Object>(props); + + // service information of this JcrResourceResolverFactoryImpl service + if (sourceService != null) { + table.put(EventConstants.SERVICE, sourceService); + table.put( + EventConstants.SERVICE_ID, + sourceService.getProperty(org.osgi.framework.Constants.SERVICE_ID)); + table.put( + EventConstants.SERVICE_OBJECTCLASS, + sourceService.getProperty(org.osgi.framework.Constants.OBJECTCLASS)); + if (sourceService.getProperty(org.osgi.framework.Constants.SERVICE_PID) != null) { + table.put( + EventConstants.SERVICE_PID, + sourceService.getProperty(org.osgi.framework.Constants.SERVICE_PID)); + } + } + + // source bundle information (if available) + if (sourceBundle != null) { + table.put(EventConstants.BUNDLE_SYMBOLICNAME, + sourceBundle.getSymbolicName()); + } + + // timestamp the event + table.put(EventConstants.TIMESTAMP, + new Long(System.currentTimeMillis())); + + // create the event + return new Event(eventName, table); + } + +} diff --git a/src/main/resources/META-INF/DISCLAIMER b/src/main/resources/META-INF/DISCLAIMER new file mode 100644 index 0000000..90850c2 --- /dev/null +++ b/src/main/resources/META-INF/DISCLAIMER @@ -0,0 +1,7 @@ +Apache Sling is an effort undergoing incubation at The Apache Software Foundation (ASF), +sponsored by the Apache Jackrabbit PMC. Incubation is required of all newly accepted +projects until a further review indicates that the infrastructure, communications, +and decision making process have stabilized in a manner consistent with other +successful ASF projects. While incubation status is not necessarily a reflection of +the completeness or stability of the code, it does indicate that the project has yet +to be fully endorsed by the ASF. \ No newline at end of file diff --git a/src/main/resources/META-INF/LICENSE b/src/main/resources/META-INF/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/src/main/resources/META-INF/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/src/main/resources/META-INF/NOTICE b/src/main/resources/META-INF/NOTICE new file mode 100644 index 0000000..5fb8b10 --- /dev/null +++ b/src/main/resources/META-INF/NOTICE @@ -0,0 +1,9 @@ +Apache Sling Commons OSGi +Copyright 2008-2009 The Apache Software Foundation + +Apache Sling is based on source code originally developed +by Day Software (http://www.day.com/). + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + diff --git a/src/test/java/org/apache/sling/commons/osgi/ManifestHeaderTest.java b/src/test/java/org/apache/sling/commons/osgi/ManifestHeaderTest.java new file mode 100644 index 0000000..f1b0d5b --- /dev/null +++ b/src/test/java/org/apache/sling/commons/osgi/ManifestHeaderTest.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.commons.osgi; + +import junit.framework.TestCase; + +/** + * Tests for the manifest header parsing. + */ +public class ManifestHeaderTest extends TestCase { + + public void testNonExisting() { + String header = null; + final ManifestHeader entry = ManifestHeader.parse(header); + assertNull(entry); + } + + public void testSinglePath() { + String header = "something"; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(1, entry.getEntries().length); + assertEquals(header, entry.getEntries()[0].getValue()); + assertEquals(0, entry.getEntries()[0].getAttributes().length); + assertEquals(0, entry.getEntries()[0].getDirectives().length); + } + + public void testSeveralPaths() { + String header = "one,two, three ,\n four, \n five"; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(5, entry.getEntries().length); + assertEquals("one", entry.getEntries()[0].getValue()); + assertEquals(0, entry.getEntries()[0].getAttributes().length); + assertEquals(0, entry.getEntries()[0].getDirectives().length); + assertEquals("two", entry.getEntries()[1].getValue()); + assertEquals(0, entry.getEntries()[1].getAttributes().length); + assertEquals(0, entry.getEntries()[1].getDirectives().length); + assertEquals("three", entry.getEntries()[2].getValue()); + assertEquals(0, entry.getEntries()[2].getAttributes().length); + assertEquals(0, entry.getEntries()[2].getDirectives().length); + assertEquals("four", entry.getEntries()[3].getValue()); + assertEquals(0, entry.getEntries()[3].getAttributes().length); + assertEquals(0, entry.getEntries()[3].getDirectives().length); + assertEquals("five", entry.getEntries()[4].getValue()); + assertEquals(0, entry.getEntries()[4].getAttributes().length); + assertEquals(0, entry.getEntries()[4].getDirectives().length); + } + + public void testAttributes() { + String header = "one;a=1;b=2"; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(1, entry.getEntries().length); + assertEquals("one", entry.getEntries()[0].getValue()); + assertEquals(2, entry.getEntries()[0].getAttributes().length); + assertEquals(0, entry.getEntries()[0].getDirectives().length); + assertEquals("a", entry.getEntries()[0].getAttributes()[0].getName()); + assertEquals("b", entry.getEntries()[0].getAttributes()[1].getName()); + assertEquals("1", entry.getEntries()[0].getAttributes()[0].getValue()); + assertEquals("2", entry.getEntries()[0].getAttributes()[1].getValue()); + } + + public void testDirectives() { + String header = "one;a:=1;b:=2"; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(1, entry.getEntries().length); + assertEquals("one", entry.getEntries()[0].getValue()); + assertEquals(2, entry.getEntries()[0].getDirectives().length); + assertEquals(0, entry.getEntries()[0].getAttributes().length); + assertEquals("a", entry.getEntries()[0].getDirectives()[0].getName()); + assertEquals("b", entry.getEntries()[0].getDirectives()[1].getName()); + assertEquals("1", entry.getEntries()[0].getDirectives()[0].getValue()); + assertEquals("2", entry.getEntries()[0].getDirectives()[1].getValue()); + } + + public void testQuoting() { + String header = "one;a:=\"1,2\""; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(1, entry.getEntries().length); + assertEquals("one", entry.getEntries()[0].getValue()); + assertEquals(1, entry.getEntries()[0].getDirectives().length); + assertEquals(0, entry.getEntries()[0].getAttributes().length); + assertEquals("a", entry.getEntries()[0].getDirectives()[0].getName()); + assertEquals("1,2", entry.getEntries()[0].getDirectives()[0].getValue()); + } + + public void testQuoting2() { + String header = "CQ-INF/content/apps/xyz/docroot;overwrite:=true;path:=/apps/xyz/docroot;ignoreImportProviders:=\"json,xml\""; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(1, entry.getEntries().length); + assertEquals("CQ-INF/content/apps/xyz/docroot", entry.getEntries()[0].getValue()); + assertEquals(3, entry.getEntries()[0].getDirectives().length); + assertEquals(0, entry.getEntries()[0].getAttributes().length); + assertEquals("overwrite", entry.getEntries()[0].getDirectives()[0].getName()); + assertEquals("true", entry.getEntries()[0].getDirectives()[0].getValue()); + assertEquals("path", entry.getEntries()[0].getDirectives()[1].getName()); + assertEquals("/apps/xyz/docroot", entry.getEntries()[0].getDirectives()[1].getValue()); + assertEquals("ignoreImportProviders", entry.getEntries()[0].getDirectives()[2].getName()); + assertEquals("json,xml", entry.getEntries()[0].getDirectives()[2].getValue()); + } + + + public void testMultipleEntries() { + String header = "SLING-INF/content/etc;checkin:=true;path:=/etc,\nSLING-INF/content/libs;overwrite:=true;path:=/libs"; + final ManifestHeader entry = ManifestHeader.parse(header); + assertEquals(2, entry.getEntries().length); + assertEquals("SLING-INF/content/etc", entry.getEntries()[0].getValue()); + assertEquals(2, entry.getEntries()[0].getDirectives().length); + assertEquals(0, entry.getEntries()[0].getAttributes().length); + assertEquals("checkin", entry.getEntries()[0].getDirectives()[0].getName()); + assertEquals("path", entry.getEntries()[0].getDirectives()[1].getName()); + assertEquals("true", entry.getEntries()[0].getDirectives()[0].getValue()); + assertEquals("/etc", entry.getEntries()[0].getDirectives()[1].getValue()); + assertEquals("SLING-INF/content/libs", entry.getEntries()[1].getValue()); + assertEquals(2, entry.getEntries()[1].getDirectives().length); + assertEquals(0, entry.getEntries()[1].getAttributes().length); + assertEquals("overwrite", entry.getEntries()[1].getDirectives()[0].getName()); + assertEquals("path", entry.getEntries()[1].getDirectives()[1].getName()); + assertEquals("true", entry.getEntries()[1].getDirectives()[0].getValue()); + assertEquals("/libs", entry.getEntries()[1].getDirectives()[1].getValue()); + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
