/** * */ package org.gcube.portlets.user.workspace.server.notifications; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Timer; import java.util.TimerTask; import org.apache.log4j.Logger; import org.gcube.common.core.contexts.GHNContext; import org.gcube.common.core.informationsystem.client.AtomicCondition; import org.gcube.common.core.informationsystem.client.ISClient; import org.gcube.common.core.informationsystem.client.queries.GCUBERIQuery; import org.gcube.common.core.scope.GCUBEScope; import com.liferay.portal.model.Organization; import com.liferay.portal.service.OrganizationLocalServiceUtil; /** * @author "Federico De Faveri defaveri@isti.cnr.it" * */ public class FeedScheduler { protected Timer scheduler; protected Map scheduledScopes; protected Logger logger = Logger.getLogger(FeedScheduler.class); public FeedScheduler(long refreshTime) { scheduledScopes = new HashMap(); scheduler = new Timer(true); scheduler.schedule(new TimerTask() { @Override public void run() { checkScopes(); } }, 0, refreshTime); } public void schedule() throws Exception { List scopes = getAvailableScopes(); for (GCUBEScope scope:scopes) { logger.trace("checking scope: "+scope); if (isServicePresentInScope(scope)) { logger.trace("service present"); schedule(scope); } else logger.trace("service not present"); } checkScopes(); } protected void schedule(GCUBEScope scope) { // if (!scheduledScopes.containsKey(scope)) { // NotificationsProducer feeder = new NotificationsProducer(scope); // scheduledScopes.put(scope, feeder); // } } protected void checkScopes() { for (NotificationsProducer feeder:scheduledScopes.values()) { try { // feeder.checkOperatorsForFeed(); } catch (Exception e) { } } } protected boolean isServicePresentInScope(GCUBEScope scope) throws Exception { ISClient isClient = GHNContext.getImplementation(ISClient.class); GCUBERIQuery query = isClient.getQuery(GCUBERIQuery.class); query.addAtomicConditions(new AtomicCondition("/Profile/ServiceName", "statistical-manager-gcubews")); query.addAtomicConditions(new AtomicCondition("/Profile/ServiceClass", "DataAnalysis")); return isClient.execute(query, scope).size()>0; } protected static List getAvailableScopes() throws Exception { //FIXME for test only //return Arrays.asList(GCUBEScope.getScope("/gcube/devsec/devVRE")); GHNContext ctx = GHNContext.getContext(); String rootScopeName = (String) ctx.getProperty(GHNContext.INFRASTRUCTURE_NAME, true); GCUBEScope rootScope = GCUBEScope.getScope("/"+rootScopeName); List scopes = findAvailableScopes(rootScope); return scopes; } protected static List findAvailableScopes(GCUBEScope infrastructure) throws Exception { List scopes = new ArrayList(); //************* PORTAL MODE, Checking organizations scopes.add(infrastructure); ///************* GET ROOT ORGANIZATION List organizations = OrganizationLocalServiceUtil.getOrganizations(0, OrganizationLocalServiceUtil.getOrganizationsCount()); Organization rootOrganization = null; for (Organization organization : organizations) { if (organization.getName().equals(infrastructure.getName()) ) { rootOrganization = organization; break; } } if(rootOrganization==null) throw new Exception("Unable to find infrastructure scope "+infrastructure.getName()+" among organizations"); //************** GET VO for (Organization vOrg : rootOrganization.getSuborganizations()){ String VOScopeString="/"+vOrg.getParentOrganization().getName()+"/"+vOrg.getName(); try{ scopes.add(GCUBEScope.getScope(VOScopeString)); for (Organization vre : vOrg.getSuborganizations()){ String VREScopeString=VOScopeString+"/"+vre.getName(); try{ scopes.add(GCUBEScope.getScope(VREScopeString)); }catch(Exception e){ } } }catch(Exception e){ } //************* GET VRE } return scopes; } public static void main(String[] args) throws Exception { FeedScheduler scheduler = new FeedScheduler(1000); scheduler.schedule(); } }