8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The contents of this file are subject to the terms
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * of the Common Development and Distribution License
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * (the License). You may not use this file except in
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * compliance with the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * You can obtain a copy of the License at
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * See the License for the specific language governing
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * permission and limitations under the License.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * When distributing Covered Code, include this CDDL
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * Header Notice in each file and include the License file
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * If applicable, add the following below the CDDL Header,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * with the fields enclosed by brackets [] replaced by
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * your own identifying information:
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * $Id: ServiceInstanceImpl.java,v 1.6 2008/07/11 01:46:20 arviranga Exp $
bee2440354b4bc8796e1de0b6cbd60e1f68deba0Phill Cunnington * Portions Copyrighted 2011-2015 ForgeRock AS.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * The class <code>ServiceInstanceImpl</code> provides methods to get
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster * service's instance variables.
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Cached SMS entry
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Instance attributes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private ServiceInstanceImpl(String name, CachedSMSEntry entry) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sb.append("\nService Instance: ").append(name).append("\n\tGroup: ")
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(getGroup()).append("\n\tURI: ").append(getURI())
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append("\n\tAttributes: ").append(attributes);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster void refresh(SMSEntry newEntry) throws SMSException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Gets calls by local changes and also by notifications threads
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Hence synchronized to avoid data corruption
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Read the attributes
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster attributes = SMSUtils.getAttrsFromEntry(entry);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the group attribute
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] groups = entry.getAttributeValues(SMSEntry.ATTR_SERVICE_ID);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Get the URI
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String[] uris = entry.getAttributeValues(SMSEntry.ATTR_LABELED_URI);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (smsEntry.isValid() && smsEntry.isDirty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // ----------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Protected static methods
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // ----------------------------------------------------------
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static ServiceInstanceImpl getInstance(SSOToken token, String serviceName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String version, String iName) throws SMSException, SSOException {
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace //construct instance DN using default realm
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace return getInstance(token, serviceName, version, iName, null);
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace static ServiceInstanceImpl getInstance(SSOToken token, String serviceName,
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace String version, String iName, String oName) throws SMSException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("ServiceInstanceImpl::getInstance: called: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceName + "(" + version + ")" + " Instance: " + iName);
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace String cName = getCacheName(serviceName, version, iName, oName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check the cache
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceInstanceImpl answer = getFromCache(cName, serviceName, version,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if the entry has to be updated
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!SMSEntry.cacheSMSEntries || answer.smsEntry.isDirty()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Since the SMSEntries are not to be cached, read the entry
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Construct the service instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (serviceInstances) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check cache again, in case it was added by another thread
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if ((answer = getFromCache(cName, serviceName, version, iName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Still not present in cache, create and add to cache
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster CachedSMSEntry entry = checkAndUpdatePermission(cName,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster answer = new ServiceInstanceImpl(iName, entry);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster debug.message("ServiceInstanceImpl::getInstance: success: " +
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster serviceName + "(" + version + ")" + " Instance: " + iName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Clears the cache
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster synchronized (serviceInstances) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster for (Iterator items = serviceInstances.values().iterator();
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceInstanceImpl impl = (ServiceInstanceImpl) items.next();
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace static String getCacheName(String sName, String version, String ins, String oName) {
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace sb.append(sName).append(version).append(ins).append(oName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static ServiceInstanceImpl getFromCache(String cacheName, String sName,
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace String version, String iName, String oName, SSOToken t) throws SMSException,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster ServiceInstanceImpl answer = (ServiceInstanceImpl) serviceInstances
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (answer != null && !answer.smsEntry.isValid()) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // CachedSMSEntry is invalid. Recreate this instance
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if the user has permissions
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set principals = (Set) userPrincipals.get(cacheName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster if (!principals.contains(t.getTokenID().toString())) {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Check if Principal has permission to read entry
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace checkAndUpdatePermission(cacheName, sName, version, iName, oName, t);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster static synchronized CachedSMSEntry checkAndUpdatePermission(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster String cacheName, String serviceName, String version, String iName,
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace String oName, SSOToken t) throws SMSException, SSOException {
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster // Construct the DN
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace // OPENAM-3269
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace // commenting out since it always construct DN with default realm
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace // String dn = "ou=" + iName + "," + CreateServiceConfig.INSTANCES_NODE
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace // + ServiceManager.getServiceNameDN(serviceName, version);
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace String dn = constructServiceInstanceDN(serviceName, version, iName, oName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster CachedSMSEntry entry = CachedSMSEntry.getInstance(t, dn);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster throw (new SMSException(IUMSConstants.UMS_BUNDLE_NAME,
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster Set sudoPrincipals = (Set) userPrincipals.get(cacheName);
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster sudoPrincipals = Collections.synchronizedSet(new HashSet());
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace private static String constructServiceInstanceDN(String serviceName, String version,
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace String instanceName, String orgName) throws SMSException {
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace sb.append("ou=").append(instanceName).append(SMSEntry.COMMA).append(
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace CreateServiceConfig.INSTANCES_NODE).append("ou=").append(version)
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace .append(SMSEntry.COMMA).append("ou=").append(serviceName)
793a02c1ab0479a0b1abb478e028814b55bb151aSachiko Wallace .append(SMSEntry.COMMA).append(SMSEntry.SERVICES_RDN).append(
684bd353e40f5162c740ab7bf0cb8c4f425da182Sachiko Wallace //DNMapper will map null or empty string to baseDN
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Map serviceInstances = Collections.synchronizedMap(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster private static Map userPrincipals = Collections.synchronizedMap(
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(" ").append(SMSUtils.NAME).append("=\"").append(name)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster .append(" ").append(SMSUtils.GROUP).append("=\"").append(group)
8af80418ba1ec431c8027fa9668e5678658d3611Allan Foster buff.append(" ").append(SMSUtils.URI).append("=\"").append(uri)