All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.role.RoleSet Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.role;

import org.netbeans.platformx.inject.api.Injector;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.NotThreadSafe;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.util.lookup.ProxyLookup;
import it.tidalwave.util.logging.Logger;

/***********************************************************************************************************************
 *
 * This class models a set of (possibly cooperating) roles member of a DCI pattern. Roles can be static (if they will be
 * always part of this set, typically defined in a configuration file) or dynamic (if they can be programmatically
 * added and removed at any time). This class provides role initialization inclusive of resource injection and call of
 * methods annotated with {@link @PostConstruct}. The whole set of roles is made available by means of a {@link Lookup}.
 *
 * A typical use is:
 *
 * 
 * RoleSet roleSet = new RoleSet();
 *
 * roleSet.setInjectedLookup(roleSet.getLookup());
 * // or alternatively
 * roleSet.setInjectedLookup(new ProxyLookup(myLookup, roleSet.getLookup());
 * 
 * roleSet.setStaticRoles(role1, role2);
 * roleSet.initialize();
 * ...
 * roleSet.addRole(role3);
 * ...
 * roleSet.removeRole(role3);
 * 
* * @author Fabrizio Giudici * @version $Id$ * **********************************************************************************************************************/ @NotThreadSafe public class RoleSet implements Lookup.Provider { private final static String CLASS = RoleSet.class.getName(); private final static Logger logger = Logger.getLogger(CLASS); private final InstanceContent dynamicRoles = new InstanceContent(); private final InstanceContent staticRoles = new InstanceContent(); private final AbstractLookup dynamicRolesLookup = new AbstractLookup(dynamicRoles); private final AbstractLookup staticRolesLookup = new AbstractLookup(staticRoles); private final Lookup lookup = new ProxyLookup(dynamicRolesLookup, staticRolesLookup); private boolean setupPerformed = false; private Lookup injectedLookup = Lookup.EMPTY; /******************************************************************************************************************* * * Sets the {@link Lookup} instances that will be injected to roles. * * @param injectedLookup the {@code Lookup} that will be injected * ******************************************************************************************************************/ public void setInjectedLookup (final @Nonnull Lookup injectedLookup) { this.injectedLookup = injectedLookup; } /******************************************************************************************************************* * * Returns a {@link Lookup} which contains all the roles in this set (both static and dynamic}. * ******************************************************************************************************************/ @Override @Nonnull public Lookup getLookup() { return lookup; } /******************************************************************************************************************* * * Programmatically adds a new role to this set. If this method is called before the initialization of the set, * the role initialization will be postponed; otherwise, it will be performed immediately. * * @param role the role * ******************************************************************************************************************/ public void addRole (@Nonnull final Object role) { // // It is allowed to call this method at any time, still Behaviours will be all initialized // in initialize(). Once initialization has been completed, Behaviours are installed as they // are added. // if (setupPerformed) { Injector.getDefault().inject(role, injectedLookup); } dynamicRoles.add(role); } /******************************************************************************************************************* * * Programmatically removes a role from this set. * * @param role the role to remove * ******************************************************************************************************************/ public void removeRole (@Nonnull final Object role) { dynamicRoles.remove(role); } /******************************************************************************************************************* * * Sets the static roles, that is roles that can't be removed from this set. * * @param staticRoles the static roles * ******************************************************************************************************************/ public void setStaticRoles (final @Nonnull Collection staticRoles) { for (final Object role : staticRoles) { this.staticRoles.add(role); } } /******************************************************************************************************************* * * Initializes this set. This method performs dependency injections and calls methods annotated with * {@link @PostConstruct}. Roles are initialized in dependency order - that is, when a role is initialized, it is * guaranteed that all the injected roles and resources have been initialized too. * ******************************************************************************************************************/ @edu.umd.cs.findbugs.annotations.SuppressWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON") public void initialize() { final List rolesToInitialize = new ArrayList(); rolesToInitialize.addAll(staticRolesLookup.lookupAll(Object.class)); rolesToInitialize.addAll(dynamicRolesLookup.lookupAll(Object.class)); Injector.getDefault().inject(rolesToInitialize, injectedLookup); setupPerformed = true; } }