Java Code Examples for org.osgi.framework.FrameworkUtil#createFilter()

The following examples show how to use org.osgi.framework.FrameworkUtil#createFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BundleLocationCondition.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Constructs a condition that tries to match the passed Bundle's location
 * to the location pattern.
 * 
 * @param bundle The Bundle being evaluated.
 * @param info The ConditionInfo from which to construct the condition. The
 *        ConditionInfo must specify one or two arguments. The first
 *        argument of the ConditionInfo specifies the location pattern
 *        against which to match the bundle location. Matching is done
 *        according to the filter string matching rules. Any '*' characters
 *        in the first argument are used as wildcards when matching bundle
 *        locations unless they are escaped with a '\' character. The
 *        Condition is satisfied if the bundle location matches the pattern.
 *        The second argument of the ConditionInfo is optional. If a second
 *        argument is present and equal to "!", then the satisfaction of the
 *        Condition is negated. That is, the Condition is satisfied if the
 *        bundle location does NOT match the pattern. If the second argument
 *        is present but does not equal "!", then the second argument is
 *        ignored.
 * @return Condition object for the requested condition.
 */
static public Condition getCondition(final Bundle bundle, final ConditionInfo info) {
	if (!CONDITION_TYPE.equals(info.getType()))
		throw new IllegalArgumentException("ConditionInfo must be of type \"" + CONDITION_TYPE + "\"");
	String[] args = info.getArgs();
	if (args.length != 1 && args.length != 2)
		throw new IllegalArgumentException("Illegal number of args: " + args.length);
	String bundleLocation = AccessController.doPrivileged(new PrivilegedAction<String>() {
		public String run() {
			return bundle.getLocation();
		}
	});
	Filter filter = null;
	try {
		filter = FrameworkUtil.createFilter("(location=" + escapeLocation(args[0]) + ")");
	} catch (InvalidSyntaxException e) {
		// this should never happen, but just in case
		throw new RuntimeException("Invalid filter: " + e.getFilter(), e);
	}
	Dictionary<String, String> matchProps = new Hashtable<String, String>(2);
	matchProps.put("location", bundleLocation);
	boolean negate = (args.length == 2) ? "!".equals(args[1]) : false;
	return (negate ^ filter.match(matchProps)) ? Condition.TRUE : Condition.FALSE;
}
 
Example 2
Source File: NativeRequirement.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private Filter toFilter(List<String> procs, List<String> oses, List<VersionRange> vers, List<String> langs, String sf)
  throws InvalidSyntaxException
{
  final StringBuffer sb = new StringBuffer(80);
  int elems = 0;
  
  elems = andAdd(sb, orString(NativeNamespace.CAPABILITY_PROCESSOR_ATTRIBUTE, procs));
  elems += andAdd(sb, orString(NativeNamespace.CAPABILITY_OSNAME_ATTRIBUTE, oses));
  elems += andAdd(sb, orString(NativeNamespace.CAPABILITY_OSVERSION_ATTRIBUTE, vers));
  elems += andAdd(sb, orString(NativeNamespace.CAPABILITY_LANGUAGE_ATTRIBUTE, langs));
  elems += andAdd(sb, sf);
  if (elems == 0) {
    return null;
  } else if (elems > 1) {
    sb.insert(0,"(&");
    sb.append(")");
  }
  return FrameworkUtil.createFilter(sb.toString());
}
 
Example 3
Source File: EndpointPermission.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Parse filter string into a Filter object.
 * 
 * @param filterString The filter string to parse.
 * @return a Filter for this bundle.
 * @throws IllegalArgumentException If the filter syntax is invalid.
 */
private static Filter parseFilter(String filterString) {
	if (filterString == null) {
		throw new IllegalArgumentException("invalid filter: null");
	}
	filterString = filterString.trim();
	if (filterString.equals("*")) {
		return null; // wildcard
	}
	try {
		return FrameworkUtil.createFilter(filterString);
	} catch (InvalidSyntaxException e) {
		IllegalArgumentException iae = new IllegalArgumentException("invalid filter");
		iae.initCause(e);
		throw iae;
	}
}
 
Example 4
Source File: BundleLocationCondition.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Constructs a condition that tries to match the passed Bundle's location
 * to the location pattern.
 * 
 * @param bundle The Bundle being evaluated.
 * @param info The ConditionInfo from which to construct the condition. The
 *        ConditionInfo must specify one or two arguments. The first
 *        argument of the ConditionInfo specifies the location pattern
 *        against which to match the bundle location. Matching is done
 *        according to the filter string matching rules. Any '*' characters
 *        in the first argument are used as wildcards when matching bundle
 *        locations unless they are escaped with a '\' character. The
 *        Condition is satisfied if the bundle location matches the pattern.
 *        The second argument of the ConditionInfo is optional. If a second
 *        argument is present and equal to "!", then the satisfaction of the
 *        Condition is negated. That is, the Condition is satisfied if the
 *        bundle location does NOT match the pattern. If the second argument
 *        is present but does not equal "!", then the second argument is
 *        ignored.
 * @return Condition object for the requested condition.
 */
static public Condition getCondition(final Bundle bundle, final ConditionInfo info) {
	if (!CONDITION_TYPE.equals(info.getType()))
		throw new IllegalArgumentException("ConditionInfo must be of type \"" + CONDITION_TYPE + "\"");
	String[] args = info.getArgs();
	if (args.length != 1 && args.length != 2)
		throw new IllegalArgumentException("Illegal number of args: " + args.length);
	String bundleLocation = AccessController.doPrivileged(new PrivilegedAction<String>() {
		public String run() {
			return bundle.getLocation();
		}
	});
	Filter filter = null;
	try {
		filter = FrameworkUtil.createFilter("(location=" + escapeLocation(args[0]) + ")");
	} catch (InvalidSyntaxException e) {
		// this should never happen, but just in case
		throw new RuntimeException("Invalid filter: " + e.getFilter(), e);
	}
	Dictionary<String, String> matchProps = new Hashtable<String, String>(2);
	matchProps.put("location", bundleLocation);
	boolean negate = (args.length == 2) ? "!".equals(args[1]) : false;
	return (negate ^ filter.match(matchProps)) ? Condition.TRUE : Condition.FALSE;
}
 
Example 5
Source File: BundleLocationCondition.java    From concierge with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Constructs a condition that tries to match the passed Bundle's location
 * to the location pattern.
 * 
 * @param bundle The Bundle being evaluated.
 * @param info The ConditionInfo from which to construct the condition. The
 *        ConditionInfo must specify one or two arguments. The first
 *        argument of the ConditionInfo specifies the location pattern
 *        against which to match the bundle location. Matching is done
 *        according to the filter string matching rules. Any '*' characters
 *        in the first argument are used as wildcards when matching bundle
 *        locations unless they are escaped with a '\' character. The
 *        Condition is satisfied if the bundle location matches the pattern.
 *        The second argument of the ConditionInfo is optional. If a second
 *        argument is present and equal to "!", then the satisfaction of the
 *        Condition is negated. That is, the Condition is satisfied if the
 *        bundle location does NOT match the pattern. If the second argument
 *        is present but does not equal "!", then the second argument is
 *        ignored.
 * @return Condition object for the requested condition.
 */
static public Condition getCondition(final Bundle bundle, final ConditionInfo info) {
	if (!CONDITION_TYPE.equals(info.getType()))
		throw new IllegalArgumentException("ConditionInfo must be of type \"" + CONDITION_TYPE + "\"");
	String[] args = info.getArgs();
	if (args.length != 1 && args.length != 2)
		throw new IllegalArgumentException("Illegal number of args: " + args.length);
	String bundleLocation = AccessController.doPrivileged(new PrivilegedAction<String>() {
		public String run() {
			return bundle.getLocation();
		}
	});
	Filter filter = null;
	try {
		filter = FrameworkUtil.createFilter("(location=" + escapeLocation(args[0]) + ")");
	} catch (InvalidSyntaxException e) {
		// this should never happen, but just in case
		throw new RuntimeException("Invalid filter: " + e.getFilter(), e);
	}
	Dictionary<String, String> matchProps = new Hashtable<String, String>(2);
	matchProps.put("location", bundleLocation);
	boolean negate = (args.length == 2) ? "!".equals(args[1]) : false;
	return (negate ^ filter.match(matchProps)) ? Condition.TRUE : Condition.FALSE;
}
 
Example 6
Source File: FilterUtil.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public static Filter createSimpleOr ( final String attribute, final Set<String> values ) throws InvalidSyntaxException
{
    final StringBuilder sb = new StringBuilder ();

    sb.append ( "(|" );

    for ( final String value : values )
    {
        addPair ( sb, attribute, value );
    }

    sb.append ( ")" );

    return FrameworkUtil.createFilter ( sb.toString () );
}
 
Example 7
Source File: ReferenceDescription.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
ReferenceDescription(String name,
                     String interfaceName,
                     boolean optional,
                     boolean multiple,
                     boolean dynamic,
                     boolean greedy,
                     String target,
                     String bind,
                     String unbind,
                     String updated,
                     String scope,
                     String field,
                     Boolean fieldUpdate,
                     int fieldCollectionType)
  throws InvalidSyntaxException
{
  targetFilter = (target != null) ? FrameworkUtil.createFilter(target) : null;
  this.name = name;
  this.interfaceName = interfaceName;
  this.optional = optional;
  this.multiple = multiple;
  this.dynamic = dynamic;
  this.greedy = greedy;
  this.bind = bind;
  this.unbind = unbind;
  this.updated = updated;
  this.scope = scope;
  this.field = field;
  this.fieldUpdate = fieldUpdate;
  this.fieldCollectionType = fieldCollectionType;
}
 
Example 8
Source File: BundleRequirementImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Creates a {@link BundleRequirement} from one entry in the parsed
 * "Require-Capability" manifest header.
 *
 * @param gen
 *          the owning bundle revision.
 * @param he
 *          the parsed entry from the "Require-Capability" manifest header.
 */
BundleRequirementImpl(final BundleGeneration gen, final HeaderEntry he)
{
  this.gen = gen;
  namespace = he.getKey();
  for (final String ns : Arrays
      .asList(new String[] { BundleRevision.BUNDLE_NAMESPACE,
                             BundleRevision.HOST_NAMESPACE,
                             BundleRevision.PACKAGE_NAMESPACE })) {
    if (ns.equals(namespace)) {
      throw new IllegalArgumentException("Capability with name-space '" + ns
                                         + "' must not be required in the "
                                         + Constants.REQUIRE_CAPABILITY
                                         + " manifest header.");
    }
  }

  final String filterStr = he.getDirectives().remove("filter");
  if (null!=filterStr && filterStr.length()>0) {
    try {
      filter = FrameworkUtil.createFilter(filterStr);
      he.getDirectives().put("filter", filter.toString());
    } catch (final InvalidSyntaxException ise) {
      final String msg = "Invalid filter '" + filterStr + "' in "
                         + Constants.REQUIRE_CAPABILITY
                         + " for name-space " + namespace + ": " + ise;
      throw (IllegalArgumentException)
        new IllegalArgumentException(msg).initCause(ise);
    }
  } else {
    filter = null;
  }
  directives = Collections.unmodifiableMap(he.getDirectives());
  attributes = Collections.unmodifiableMap(he.getAttributes());
}
 
Example 9
Source File: DelayedProbeInvokerFactory.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return LDAP filter created by formatting the given arguments
 */
private static Filter filter(final String format, final Object... args) {
  try {
    return FrameworkUtil.createFilter(String.format(format, args));
  }
  catch (final InvalidSyntaxException e) {
    throw new IllegalArgumentException(e);
  }
}
 
Example 10
Source File: ApplicationAdminPermission.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Filter getFilter() {
	if (appliedFilter == null) {
		try {
			appliedFilter = FrameworkUtil.createFilter(filter);
		} catch (InvalidSyntaxException e) {
			// we will return null
		}
	}
	return appliedFilter;
}
 
Example 11
Source File: FrameworkWiringImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Collection<BundleCapability> findProviders(Requirement requirement) {
  final String namespace = requirement.getNamespace();
  final String filterStr = requirement.getDirectives().get("filter");
  Filter filter;
  if (filterStr != null) {
    try {
      filter = FrameworkUtil.createFilter(filterStr);
    } catch (InvalidSyntaxException ise) {
      final String msg = "Invalid filter directive '" + filterStr + "': " + ise;
      throw new IllegalArgumentException(msg, ise);
    }
  } else {
    filter = null;
  }
  HashSet<BundleCapability> res = new HashSet<BundleCapability>();
  for (BundleGeneration bg : fwCtx.bundles.getBundleGenerations(null)) {
    BundleRevisionImpl bri = bg.bundleRevision;
    if (bri != null) {
      for (BundleCapability bc : bri.getDeclaredCapabilities(namespace)) {
        if (null == filter || filter.matches(bc.getAttributes())) {
          res.add(bc);
        }
      }
    }
  }
  return res;
}
 
Example 12
Source File: RepositoryCommandGroup.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int cmdShow(Dictionary<String,?> opts, Reader in, PrintWriter out,
    Session session) {
  final String namespace = (String) opts.get("namespace");
  final String filterStr = (String) opts.get("filter");

  BasicRequirement requirement;
  requirement = new BasicRequirement(namespace);
  if (filterStr != null) {
    try {
      FrameworkUtil.createFilter(filterStr);
    } catch (InvalidSyntaxException e) {
      out.println("Invalid filter: " + e.getMessage());
      return 1;
    }
    requirement.addDirective("filter", filterStr); 
  }
  List<Capability> cs = getRepositoryManager().findProviders(requirement);
  if (cs.isEmpty()) {
    out.println("No matching resources found!");
    return 0;
  }
  List<Resource> resources = new ArrayList<Resource>();
  for (Capability c : cs) {
    resources.add(c.getResource());
  }
  printResources(out, resources, namespace, opts.get("-t") == null);
  return 0;
}
 
Example 13
Source File: SubsystemPermission.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Parse filter string into a Filter object.
 * 
 * @param filterString The filter string to parse.
 * @return a Filter for this subsystem. If the specified filterString equals
 *         "*", then {@code null} is returned to indicate a wildcard.
 * @throws IllegalArgumentException If the filter syntax is invalid.
 */
private static Filter parseFilter(String filterString) {
	filterString = filterString.trim();
	if (filterString.equals("*")) {
		return null;
	}

	try {
		return FrameworkUtil.createFilter(filterString);
	} catch (InvalidSyntaxException e) {
		IllegalArgumentException iae = new IllegalArgumentException("invalid filter");
		iae.initCause(e);
		throw iae;
	}
}
 
Example 14
Source File: DevicePermission.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Filter parseFilter(String filter) {
	if ((1 == filter.length()) && ("*".equals(filter))) {
		return null;
	}
	try {
		return FrameworkUtil.createFilter(filter);
	} catch (InvalidSyntaxException ise) {
		IllegalArgumentException iae = new IllegalArgumentException("The filter is invalid: " + filter);
		iae.initCause(ise);
		throw iae;
	}
}
 
Example 15
Source File: FilterUtil.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
protected static Filter createFilter ( final String operand, final Map<String, String> parameters ) throws InvalidSyntaxException
{
    final StringBuilder sb = new StringBuilder ();

    sb.append ( "(" );
    sb.append ( operand );

    for ( final Map.Entry<String, String> entry : parameters.entrySet () )
    {
        addPair ( sb, entry.getKey (), entry.getValue () );
    }
    sb.append ( ")" );

    return FrameworkUtil.createFilter ( sb.toString () );
}
 
Example 16
Source File: Fragment.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Filter toFilter()
{
  final StringBuffer sb = new StringBuffer(80);
  boolean multipleConditions = false;

  sb.append('(');
  sb.append(BundleRevision.HOST_NAMESPACE);
  sb.append('=');
  if (Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(hostName)) {
    sb.append(BundleGeneration.KNOPFLERFISH_SYMBOLICNAME);
  } else {
    sb.append(hostName);
  }
  sb.append(')');

  if (versionRange != null) {
    sb.append(versionRange.toFilterString(Constants.BUNDLE_VERSION_ATTRIBUTE));
    multipleConditions = true;
  }

  for (final Entry<String,Object> entry : attributes.entrySet()) {
    sb.append('(');
    sb.append(entry.getKey());
    sb.append('=');
    sb.append(entry.getValue().toString());
    sb.append(')');
    multipleConditions |= true;
  }

  if (multipleConditions) {
    sb.insert(0, "(&");
    sb.append(')');
  }
  try {
    return FrameworkUtil.createFilter(sb.toString());
  } catch (final InvalidSyntaxException _ise) {
    // Should not happen...
    System.err.println("createFilter: '" +sb.toString() +"': " +_ise.getMessage());
    return null;
  }
}
 
Example 17
Source File: FilterTracker.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public FilterTracker(BundleContext bundleContext, String name) throws InvalidSyntaxException {
  super(bundleContext, FrameworkUtil.createFilter(String.format(QUERY, name)), null);
}
 
Example 18
Source File: ListenerTracker.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public ListenerTracker(BundleContext bundleContext, String name, ServletContext servletContext)
    throws InvalidSyntaxException
{
  super(bundleContext, FrameworkUtil.createFilter(String.format(QUERY, name)), null);
  this.servletContext = servletContext;
}
 
Example 19
Source File: ImportPkg.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Filter toFilter()
{
  final StringBuffer sb = new StringBuffer(80);
  boolean multipleConditions = false;

  sb.append('(');
  sb.append(BundleRevision.PACKAGE_NAMESPACE);
  sb.append('=');
  sb.append(name);
  if (name.length()==0 || name.endsWith(".")) {
    // Dynamic import with wild-card.
    sb.append('*');
  }
  sb.append(')');

  if (packageRange != null) {
    sb.append(packageRange.toFilterString(Constants.VERSION_ATTRIBUTE));
    multipleConditions = true;
  }

  if (bundleSymbolicName != null) {
    sb.append('(');
    sb.append(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE);
    sb.append('=');
    sb.append(bundleSymbolicName);
    sb.append(')');
    multipleConditions |= true;
  }

  if (bundleRange != null) {
    sb.append(bundleRange.toFilterString(Constants.BUNDLE_VERSION_ATTRIBUTE));
    multipleConditions = true;
  }

  for (final Entry<String,Object> entry : attributes.entrySet()) {
    sb.append('(');
    sb.append(entry.getKey());
    sb.append('=');
    sb.append(entry.getValue().toString());
    sb.append(')');
    multipleConditions |= true;
  }

  if (multipleConditions) {
    sb.insert(0, "(&");
    sb.append(')');
  }
  try {
    return FrameworkUtil.createFilter(sb.toString());
  } catch (final InvalidSyntaxException _ise) {
    // Should not happen...
    System.err.println("createFilter: '" +sb.toString() +"': " +_ise.getMessage());
    return null;
  }
}
 
Example 20
Source File: BundleContextImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Constructs a Filter object. This filter object may be used
 * to match a {@link ServiceReference} or a Dictionary.
 *
 * @param filter the filter string.
 * @return the Filter object encapsulating the filter string.
 * @exception InvalidSyntaxException If the filter parameter contains
 * an invalid filter string which cannot be parsed.
 *
 * @since 1.1
 */
public Filter createFilter(String filter) throws InvalidSyntaxException {
  checkValid();
  return FrameworkUtil.createFilter(filter);
}