private List /* <Configuration> */ getConfigurations(
      String filterString, String callingBundleLocation, boolean checkLocation)
      throws IOException, InvalidSyntaxException {
    Filter filter = (filterString != null) ? context.createFilter(filterString) : null;
    Iterator /* <String> */ pids = configurationDictionaryManager.listPids();
    List /* <Configuration> */ matchingConfigurations = new ArrayList();
    while (pids.hasNext()) {

      String pid = (String) pids.next();
      ConfigurationDictionary config = configurationDictionaryManager.load(pid);
      if (filter == null) {
        matchingConfigurations.add(new ConfigurationImpl(this, config));
      } else {
        if (filter.matchCase(config.toSearchableProperties())) {
          if (!checkLocation) {
            matchingConfigurations.add(new ConfigurationImpl(this, config));
          } else {
            if (callingBundleLocation.equals(config.getLocation())) {
              matchingConfigurations.add(new ConfigurationImpl(this, config));
            }
          }
        }
      }
    }
    return matchingConfigurations;
  }
  /**
   * Internal implies method. Used by the implies and the permission collection implies methods.
   *
   * @param requested The requested SubsystemPermision which has already been validated as a proper
   *     argument. The requested SubsystemPermission must not have a filter expression.
   * @param effective The effective actions with which to start.
   * @return {@code true} if the specified permission is implied by this object; {@code false}
   *     otherwise.
   */
  boolean implies0(SubsystemPermission requested, int effective) {
    /* check actions first - much faster */
    effective |= action_mask;
    final int desired = requested.action_mask;
    if ((effective & desired) != desired) {
      return false;
    }

    /* Get our filter */
    Filter f = filter;
    if (f == null) {
      // it's "*"
      return true;
    }
    /* is requested a wildcard filter? */
    if (requested.subsystem == null) {
      return false;
    }
    Map<String, Object> requestedProperties = requested.getProperties();
    if (requestedProperties == null) {
      /*
       * If the requested properties are null, then we have detected a
       * recursion getting the subsystem location. So we return true to
       * permit the subsystem location request in the SubsystemPermission
       * check up the stack to succeed.
       */
      return true;
    }
    return f.matches(requestedProperties);
  }
  /**
   * Checks if the specified {@code permission} is implied by this permission. The method returns
   * true under the following conditions:
   *
   * <UL>
   *   <LI>This permission was created by specifying a filter (see {@link
   *       #ApplicationAdminPermission(String, String)})
   *   <LI>The implied {@code otherPermission} was created for a particular {@link
   *       ApplicationDescriptor} (see {@link #ApplicationAdminPermission(ApplicationDescriptor,
   *       String)})
   *   <LI>The {@code filter} of this permission mathes the {@code ApplicationDescriptor} specified
   *       in the {@code otherPermission}. If the filter in this permission is the {@code
   *       &lt;&lt;SELF&gt;&gt;} pseudo target, then the currentApplicationId set in the {@code
   *       otherPermission} is compared to the application Id of the target {@code
   *       ApplicationDescriptor}.
   *   <LI>The list of permitted actions in this permission contains all actions required in the
   *       {@code otherPermission}
   * </UL>
   *
   * Otherwise the method returns false.
   *
   * @param otherPermission the implied permission
   * @return true if this permission implies the {@code otherPermission}, false otherwise.
   */
  public boolean implies(Permission otherPermission) {
    if (otherPermission == null) return false;

    if (!(otherPermission instanceof ApplicationAdminPermission)) return false;

    ApplicationAdminPermission other = (ApplicationAdminPermission) otherPermission;

    if (!filter.equals("*")) {
      if (other.applicationDescriptor == null) return false;

      if (filter.equals("<<SELF>>")) {
        if (other.applicationID == null) return false; /* it cannot be, this might be a bug */

        if (!other.applicationID.equals(other.applicationDescriptor.getApplicationId()))
          return false;
      } else {
        Hashtable props = new Hashtable();
        props.put("pid", other.applicationDescriptor.getApplicationId());
        props.put("signer", new SignerWrapper(other.applicationDescriptor));

        Filter flt = getFilter();
        if (flt == null) return false;

        if (!flt.match(props)) return false;
      }
    }

    if (!actionsVector.containsAll(other.actionsVector)) return false;

    return true;
  }
Ejemplo n.º 4
0
  private NetworkChannelFactory getNetworkChannelFactory(final String protocol)
      throws RemoteOSGiException {
    try {
      final Filter filter =
          RemoteOSGiActivator.getActivator()
              .getContext()
              .createFilter(
                  "(" //$NON-NLS-1$
                      + NetworkChannelFactory.PROTOCOL_PROPERTY
                      + "="
                      + protocol //$NON-NLS-1$
                      + ")"); //$NON-NLS-1$
      final ServiceReference[] refs = networkChannelFactoryTracker.getServiceReferences();

      if (refs != null) {
        for (int i = 0; i < refs.length; i++) {
          if (filter.match(refs[i])) {
            return (NetworkChannelFactory) networkChannelFactoryTracker.getService(refs[i]);
          }
        }
      }
      throw new RemoteOSGiException(
          "No NetworkChannelFactory for " //$NON-NLS-1$
              + protocol
              + " found."); //$NON-NLS-1$

    } catch (final InvalidSyntaxException e) {
      // does not happen
      e.printStackTrace();
      return null;
    }
  }
Ejemplo n.º 5
0
 public void serviceChanged(ServiceEvent serviceEvent) {
   int serviceEventType = serviceEvent.getType();
   switch (serviceEventType) {
     case ServiceEvent.REGISTERED:
       {
         ServiceReference serviceReference = serviceEvent.getServiceReference();
         Filter filter = (Filter) serviceReference.getProperty(UPnPEventListener.UPNP_FILTER);
         if (filter == null) {
           addNewListener(serviceReference);
         } else if (filter.match(properties)) {
           addNewListener(serviceReference);
         }
       }
       ;
       break;
     case ServiceEvent.MODIFIED:
       {
       }
       ;
       break;
     case ServiceEvent.UNREGISTERING:
       {
         removeListener(serviceEvent.getServiceReference());
       }
       ;
       break;
   }
 }
Ejemplo n.º 6
0
 private void createNotifier(String deviceId) {
   ServiceReference[] serviceListeners = null;
   ServiceReference serviceReference = null;
   Filter filter = null;
   String eventListenerFilter =
       "(" + Constants.OBJECTCLASS + "=" + UPnPEventListener.class.getName() + ")";
   properties = new Properties();
   properties.put(UPnPDevice.ID, deviceId);
   properties.put(UPnPService.ID, serviceId);
   try {
     serviceListeners = context.getServiceReferences(UPnPEventListener.class.getName(), null);
     if (serviceListeners != null && serviceListeners.length > 0) {
       for (int i = 0; i < serviceListeners.length; i++) {
         serviceReference = serviceListeners[i];
         filter = (Filter) serviceReference.getProperty(UPnPEventListener.UPNP_FILTER);
         if (filter == null) {
           eventListeners.add(serviceReference);
         } else if (filter.match(properties)) {
           addNewListener(serviceReference);
         }
       }
     }
   } catch (Exception e) {
   }
   try {
     String filterString = eventListenerFilter;
     context.addServiceListener(this, filterString);
   } catch (Exception e) {
   }
 }
Ejemplo n.º 7
0
 /**
  * @param ref
  * @param event
  */
 private static void match(final ServiceReference ref, final RemoteServiceEvent event) {
   final Filter filter = (Filter) ref.getProperty(RemoteServiceListener.FILTER);
   if (filter == null
       || filter.match(
           ((RemoteServiceReferenceImpl) event.getRemoteReference()).getProperties())) {
     final RemoteServiceListener listener =
         (RemoteServiceListener) remoteServiceListenerTracker.getService(ref);
     if (listener != null) {
       listener.remoteServiceEvent(event);
     }
   }
 }
 private String getFilterString(Filter baseFilter) {
   if (baseFilter != null) {
     return baseFilter.toString();
   } else {
     return null;
   }
 }
 @Override
 public Object getService(Filter filter, long timeout) throws OsgiServiceNotAvailableException {
   ServiceTracker t = new ServiceTracker(bundleContext, filter, null);
   LOGGER.debug("getting service for filter {} from tracker", filter);
   Object result = waitForServiceFromTracker(t, timeout);
   if (result == null) {
     throw new OsgiServiceNotAvailableException(
         String.format(
             "no service matching filter \"%s\" available at the time", filter.toString()));
   }
   t.close();
   return result;
 }
  /**
   * Determines the equality of two {@code SubsystemPermission} objects.
   *
   * @param obj The object being compared for equality with this object.
   * @return {@code true} if {@code obj} is equivalent to this {@code SubsystemPermission}; {@code
   *     false} otherwise.
   */
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }

    if (!(obj instanceof SubsystemPermission)) {
      return false;
    }

    SubsystemPermission sp = (SubsystemPermission) obj;

    return (action_mask == sp.action_mask)
        && ((subsystem == sp.subsystem) || ((subsystem != null) && subsystem.equals(sp.subsystem)))
        && (filter == null ? sp.filter == null : filter.equals(sp.filter));
  }
  public List<ServiceState> getServiceReferencesInternal(
      AbstractBundle bundleState, String clazz, Filter filter, boolean checkAssignable) {
    if (bundleState == null) throw new IllegalArgumentException("Null bundleState");

    List<ServiceName> serviceNames;
    if (clazz != null) {
      serviceNames = serviceNameMap.get(clazz);
      if (serviceNames == null) serviceNames = new ArrayList<ServiceName>();

      // Add potentially registered xservcie
      ServiceName xserviceName = ServiceName.of(ModuleContext.XSERVICE_PREFIX, clazz);
      ServiceController<?> xservice = serviceContainer.getService(xserviceName);
      if (xservice != null) serviceNames.add(xserviceName);
    } else {
      // [MSC-9] Add ability to query the ServiceContainer
      Set<ServiceName> allServiceNames = new HashSet<ServiceName>();
      for (List<ServiceName> auxList : serviceNameMap.values()) allServiceNames.addAll(auxList);

      serviceNames = new ArrayList<ServiceName>(allServiceNames);
    }

    if (serviceNames.isEmpty()) return Collections.emptyList();

    if (filter == null) filter = NoFilter.INSTANCE;

    List<ServiceState> result = new ArrayList<ServiceState>();
    for (ServiceName serviceName : serviceNames) {
      ServiceController<?> controller = serviceContainer.getService(serviceName);
      if (controller == null)
        throw new IllegalStateException("Cannot obtain service for: " + serviceName);

      Object value = controller.getValue();

      // Create the ServiceState on demand for an XService instance
      // [TODO] This should be done eagerly to keep the serviceId constant
      // [TODO] service events for XService lifecycle changes
      // [MSC-17] Canonical ServiceName string representation
      if (value instanceof ServiceState == false
          && serviceName.toString().contains(ModuleContext.XSERVICE_PREFIX)) {
        long serviceId = getNextServiceId();
        Bundle bundle = packageAdmin.getBundle(value.getClass());
        AbstractBundle owner = AbstractBundle.assertBundleState(bundle);
        value =
            new ServiceState(
                owner,
                serviceId,
                new ServiceName[] {serviceName},
                new String[] {clazz},
                value,
                null);
      }

      ServiceState serviceState = (ServiceState) value;
      if (filter.match(serviceState) == false) continue;

      Object rawValue = serviceState.getRawValue();

      checkAssignable &= (clazz != null);
      checkAssignable &= (bundleState.getBundleId() != 0);
      checkAssignable &= !(rawValue instanceof ServiceFactory);
      if (checkAssignable == false || serviceState.isAssignableTo(bundleState, clazz)) {
        result.add(serviceState);
      }
    }

    // Sort the result
    Collections.sort(result, ServiceReferenceComparator.getInstance());
    Collections.reverse(result);

    return Collections.unmodifiableList(result);
  }
 /**
  * Package private constructor used by SubsystemPermissionCollection.
  *
  * @param filter name filter or {@code null} for wildcard.
  * @param mask action mask
  */
 SubsystemPermission(Filter filter, int mask) {
   super((filter == null) ? "*" : filter.toString());
   setTransients(filter, mask);
   this.subsystem = null;
 }
 protected ServiceTrackerInvocationHandler(Filter filter) {
   this.tracker = new ServiceTracker(bundleContext, filter, null);
   this.info = filter.toString();
 }
 @Override
 protected boolean matches(final ServiceReference<S> capability, final Filter filter) {
   return (filter == null) || filter.match(capability);
 }
Ejemplo n.º 15
0
 /**
  * Adds a service listener to the given bundle context under the specified filter. This method
  * will deliver <em>synthetic</em> events of type <code>REGISTERED</code> for <em>all</em>
  * existing services (that match the given filter) as if the services were registered after the
  * listener registration.
  *
  * <p>This might cause problems if a service is registered between the listener registration and
  * the retrieval of existing services since the listener will receive two events for the same
  * service. For most listeners implementations however, this should not be a problem
  *
  * @param context bundle context to register the listener with
  * @param listener service listener to be registered
  * @param filter OSGi filter (given as a Filter) for registering the listener (can be <code>null
  *     </code>)
  * @see #addServiceListener(BundleContext, ServiceListener, String)
  */
 public static void addServiceListener(
     BundleContext context, ServiceListener listener, Filter filter) {
   String toStringFilter = (filter == null ? null : filter.toString());
   addServiceListener(context, listener, toStringFilter);
 }