00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.osgi.service.condpermadmin;
00020
00021 import java.security.AccessController;
00022 import java.security.PrivilegedAction;
00023 import java.util.Hashtable;
00024
00025 import org.osgi.framework.*;
00026
00033 public class BundleLocationCondition {
00034 private static final String CONDITION_TYPE = "org.osgi.service.condpermadmin.BundleLocationCondition";
00035
00050 static public Condition getCondition(final Bundle bundle, ConditionInfo info) {
00051 if (!CONDITION_TYPE.equals(info.getType()))
00052 throw new IllegalArgumentException(
00053 "ConditionInfo must be of type \"" + CONDITION_TYPE + "\"");
00054 String[] args = info.getArgs();
00055 if (args.length != 1)
00056 throw new IllegalArgumentException("Illegal number of args: "
00057 + args.length);
00058 String bundleLocation = (String) AccessController
00059 .doPrivileged(new PrivilegedAction() {
00060 public Object run() {
00061 return bundle.getLocation();
00062 }
00063 });
00064 Filter filter = null;
00065 try {
00066 filter = FrameworkUtil.createFilter("(location="
00067 + escapeLocation(args[0]) + ")");
00068 }
00069 catch (InvalidSyntaxException e) {
00070
00071 throw new RuntimeException("Invalid filter: " + e.getFilter());
00072 }
00073 Hashtable matchProps = new Hashtable(2);
00074 matchProps.put("location", bundleLocation);
00075 return filter.match(matchProps) ? Condition.TRUE : Condition.FALSE;
00076 }
00077
00078 private BundleLocationCondition() {
00079
00080 }
00081
00089 private static String escapeLocation(String value) {
00090 boolean escaped = false;
00091 int inlen = value.length();
00092 int outlen = inlen << 1;
00093
00094 char[] output = new char[outlen];
00095 value.getChars(0, inlen, output, inlen);
00096
00097 int cursor = 0;
00098 for (int i = inlen; i < outlen; i++) {
00099 char c = output[i];
00100 switch (c) {
00101 case '\\' :
00102 if (i + 1 < outlen && output[i + 1] == '*')
00103 break;
00104 case '(' :
00105 case ')' :
00106 output[cursor] = '\\';
00107 cursor++;
00108 escaped = true;
00109 break;
00110 }
00111
00112 output[cursor] = c;
00113 cursor++;
00114 }
00115
00116 return escaped ? new String(output, 0, cursor) : value;
00117 }
00118 }