Alan Davis 86f9dfea17 Merge DEV/V3.4-BUG-FIX to HEAD (28799-28800,28864,28875,28879,28916,28941,28956,28970,28993)
28993: HomeFolderProviderSynchronizer: Provider used for LDAP to continue to use username as home folder name directly under .../app:company_home/app:user_homes
   Property spaces.user_homes.regex.pattern now set to "" rather than "^(..)" which would have given a single level hash structure.
   28970: Merge DEV/ALAN/HOME_FOLDER to DEV/V3.4-BUG-FIX
      28947: - Introduction of version 2 HomeFolderProvider2 and re-factor of version 1
        so that the code used to create the folders is now in the HomeFolderManager.
      - Re-factor homeFolderProviderSynchronizer to handle HomeFolderProvider2.
      - Addition of AbstractHomeFolderProvider.V2Adaptor to allow external providers
        that extend AbstractHomeFolderPathProvider to be handled by
        homeFolderProviderSynchronizer.
      28860: Minor change to class comment and removed unused imports
      28858: ALF-4727 Hashed home folder provider added and used by default for LDAP sync users  - based on Romain Guinot work.
      ALF-7797 HomeFolderProviderSynchronizer added to move existing users (normally those added by LDAP sync) into location preferred by home folder provider.
      - HomeFolderProviderSynchronizer bug fixes
        - tenant accounts supported for first time
        - addition of a phase to create parent folders before moving home folder to avoid a race condition
        - check for conditions that would result in FileExistExceptions as we don't want a the transaction to be discarded as this results in retries.  
      - HomeFolderProviderSynchronizerTest integration test including tenant services
      - HomeFolderManager now sets the HOME_FOLDER_PROVIDER if it uses a default when HOME_FOLDER_PROVIDER is not set.
      - AbstractHomeFolderProvider clears cache when path reset as it will be invalid.
      - UIDBasedHomeFolderProvider.createNewParent creates its own mutable List as the one passed in may not be mutable.
      28580: Save code changes - added comments to do with LDAP syn overriding the HFP value and related to this added a global property to keep empty parent folders. 
      28347: HomeFolderProviderSynchronizer
      - issue to do with new run of sync having created a user via UI that has a home folder as one of the parent folders.
      - issue to do with catching exception when creating temporary folder - transaction is gone
      - give up if error in any phase
      28298: Addition of HomeFolderPathProvider (based on Romain's work) and addition of HomeFolderProviderSynchronizer.
   28956: Merged DEV to V3.4-BUG-FIX
      ALF-9428: Multitenancy users not preserved after upgrade from 3.2.2.7 to 3.4.2
                - Provide correct RunAs context in FixUserQNamesPatch batching.
   28941: ALF-9361 : CLONE -sync Flat IMAP client with Alfresco is slow and inaccurate
   28916: ALF-9421 The AlfrescoJavaScript action now includes company home in the JavaScript scope.
   28879: Fixed ALF-9296: Alfresco Dashboard: Impossible to approve/reject task from My Tasks dashlet on My Alfresco
   28875: Fixed ALF-6329: SPANISH - Share, translation on Transfer Target configuration
   28864: Message:
   ALF-9430: RuntimeExec waitForCompletion logic is obscure
    - Only a single flag 'isCompleted'
    - Set flag in try-finally
    - Added notify()
   However, the wait() code doesn't, in practice, get called because the waitForCompletion is synchronized with the run()
   and is called a while after the reading thread is triggered.  So the logic is less obscure and safer for the finally.
   28800: File for rev 28799: ALF-9240
   28799: Merged DEV to V3.4-BUG-FIX
      28797: ALF-9240: Issue with adding an aspect with large multivalued list
             Added unit test to stress, but could not reproduce

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28995 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2011-07-13 16:47:03 +00:00

468 lines
15 KiB
Java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.processor;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.admin.SysAdminParams;
import org.alfresco.repo.jscript.ScriptUrls;
import org.alfresco.scripts.ScriptException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.ScriptLocation;
import org.alfresco.service.cmr.repository.ScriptProcessor;
import org.alfresco.service.cmr.repository.ScriptService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.QName;
import org.springframework.extensions.surf.util.ParameterCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Script service implementation
*
* @author Kevin Roast
* @author Roy Wetherall
*/
public class ScriptServiceImpl implements ScriptService
{
/** Logger */
private static final Log logger = LogFactory.getLog(ScriptServiceImpl.class);
/** The name of the default script processor */
private String defaultScriptProcessor;
/** Maps containing the script processors */
private Map<String, ScriptProcessor> scriptProcessors = new HashMap<String, ScriptProcessor>(8);
private Map<String, String> scriptProcessorNamesByExtension = new HashMap<String, String>(8);
/** The node service */
private NodeService nodeService;
private SysAdminParams sysAdminParams;
/**
* Sets the name of the default script processor
*
* @param defaultScriptProcessor the name of the default script processor
*/
public void setDefaultScriptProcessor(String defaultScriptProcessor)
{
this.defaultScriptProcessor = defaultScriptProcessor;
}
/**
* Set the node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Set the sysAdminParams
*
* @param sysAdminParams the sysAdminParams
*/
public void setSysAdminParams(SysAdminParams sysAdminParams)
{
this.sysAdminParams = sysAdminParams;
}
/**
* Register a script processor
*
* @param scriptProcessor the script processor to register with the script service
*/
public void registerScriptProcessor(ScriptProcessor scriptProcessor)
{
this.scriptProcessors.put(scriptProcessor.getName(), scriptProcessor);
this.scriptProcessorNamesByExtension.put(scriptProcessor.getExtension(), scriptProcessor.getName());
}
/**
* Reset all registered script processors
*/
public void resetScriptProcessors()
{
for (ScriptProcessor p : this.scriptProcessors.values())
{
p.reset();
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(java.lang.String, java.util.Map)
*/
public Object executeScript(String scriptClasspath, Map<String, Object> model)
throws ScriptException
{
if (scriptClasspath == null)
{
throw new IllegalArgumentException("Script ClassPath is mandatory.");
}
if (logger.isDebugEnabled())
{
logger.debug("Executing script: " + scriptClasspath);
}
try
{
ScriptProcessor scriptProcessor = getScriptProcessor(scriptClasspath);
return scriptProcessor.execute(scriptClasspath, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + scriptClasspath + "': " + err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(java.lang.String, java.lang.String, java.util.Map)
*/
public Object executeScript(String engine, String scriptClasspath, Map<String, Object> model)
throws ScriptException
{
if (scriptClasspath == null)
{
throw new IllegalArgumentException("Script ClassPath is mandatory.");
}
if (logger.isDebugEnabled())
{
logger.debug("Executing script: " + scriptClasspath);
}
try
{
ScriptProcessor scriptProcessor = lookupScriptProcessor(engine);
return scriptProcessor.execute(scriptClasspath, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + scriptClasspath + "': " + err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.util.Map)
*/
public Object executeScript(NodeRef scriptRef, QName contentProp, Map<String, Object> model)
throws ScriptException
{
if (scriptRef == null)
{
throw new IllegalArgumentException("Script NodeRef is mandatory.");
}
if (logger.isDebugEnabled())
{
logger.debug("Executing script: " + scriptRef.toString());
}
try
{
ScriptProcessor scriptProcessor = getScriptProcessor(scriptRef);
return scriptProcessor.execute(scriptRef, contentProp, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + scriptRef.toString() + "': " + err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(java.lang.String, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.namespace.QName, java.util.Map)
*/
public Object executeScript(String engine, NodeRef scriptRef, QName contentProp, Map<String, Object> model)
throws ScriptException
{
if (scriptRef == null)
{
throw new IllegalArgumentException("Script NodeRef is mandatory.");
}
if (logger.isDebugEnabled())
{
logger.debug("Executing script: " + scriptRef.toString());
}
try
{
ScriptProcessor scriptProcessor = lookupScriptProcessor(engine);
return scriptProcessor.execute(scriptRef, contentProp, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + scriptRef.toString() + "': " + err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(org.alfresco.service.cmr.repository.ScriptLocation, java.util.Map)
*/
public Object executeScript(ScriptLocation location, Map<String, Object> model)
throws ScriptException
{
ParameterCheck.mandatory("Location", location);
if (logger.isDebugEnabled())
{
logger.debug("Executing script: " + location.toString());
}
try
{
ScriptProcessor scriptProcessor = getScriptProcessor(location.toString());
return scriptProcessor.execute(location, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + location.toString() + "': " + err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScript(java.lang.String, org.alfresco.service.cmr.repository.ScriptLocation, java.util.Map)
*/
public Object executeScript(String engine, ScriptLocation location, Map<String, Object> model)
throws ScriptException
{
ParameterCheck.mandatory("Location", location);
if (logger.isDebugEnabled())
{
logger.debug("Executing script: " + location.toString());
}
try
{
ScriptProcessor scriptProcessor = lookupScriptProcessor(engine);
return scriptProcessor.execute(location, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute script '" + location.toString() + "': " + err.getMessage(), err);
}
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScriptString(java.lang.String, java.util.Map)
*/
public Object executeScriptString(String script, Map<String, Object> model)
throws ScriptException
{
return executeScriptString(this.defaultScriptProcessor, script, model);
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#executeScriptString(java.lang.String, java.util.Map)
*/
public Object executeScriptString(String engine, String script, Map<String, Object> model)
throws ScriptException
{
if (script == null || script.length() == 0)
{
throw new IllegalArgumentException("Script argument is mandatory.");
}
if (logger.isDebugEnabled())
{
logger.debug("Executing script:\n" + script);
}
try
{
ScriptProcessor scriptProcessor = lookupScriptProcessor(engine);
return scriptProcessor.executeString(script, model);
}
catch (ScriptException err)
{
throw err;
}
catch (Throwable err)
{
throw new ScriptException("Failed to execute supplied script: " + err.getMessage(), err);
}
}
/**
* Helper method to lookup the script proecessor based on a name
*
* @param name the name of the script processor
* @return ScriptProcessor the script processor, default processor if no match found
*/
protected ScriptProcessor lookupScriptProcessor(String name)
{
ScriptProcessor scriptProcessor = this.scriptProcessors.get(name);
if (scriptProcessor == null)
{
scriptProcessor = this.scriptProcessors.get(this.defaultScriptProcessor);
}
return scriptProcessor;
}
/**
* Gets a scipt processor based on the node reference of a script
*
* @param scriptNode the node reference of the script
* @return ScriptProcessor the script processor
*/
protected ScriptProcessor getScriptProcessor(NodeRef scriptNode)
{
String scriptName = (String)this.nodeService.getProperty(scriptNode, ContentModel.PROP_NAME);
return getScriptProcessorImpl(scriptName);
}
/**
* Gets a script processor based on the script location string
*
* @param scriptLocation the script location
* @return ScriptProcessor the script processor
*/
protected ScriptProcessor getScriptProcessor(String scriptLocation)
{
if (scriptLocation.indexOf(StoreRef.URI_FILLER) != -1)
{
// Try and create the nodeRef
NodeRef nodeRef = new NodeRef(scriptLocation);
scriptLocation = (String)this.nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
}
return getScriptProcessorImpl(scriptLocation);
}
/**
* Gets a script processor based on the scripts file name
*
* @param scriptFileName the scripts file name
* @return ScriptProcessor the matching script processor
*/
protected ScriptProcessor getScriptProcessorImpl(String scriptFileName)
{
String engine = null;
if (scriptFileName != null)
{
String extension = getFileExtension(scriptFileName);
if (extension != null)
{
engine = this.scriptProcessorNamesByExtension.get(extension);
}
}
return lookupScriptProcessor(engine);
}
/**
* Gets the file extension of a file
*
* @param fileName the file name
* @return the file extension
*/
private String getFileExtension(String fileName)
{
String extension = null;
int index = fileName.lastIndexOf('.');
if (index > -1 && (index < fileName.length() - 1))
{
extension = fileName.substring(index + 1);
}
return extension;
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#buildCoreModel(java.util.Map)
*/
public void buildCoreModel(Map<String, Object> inputMap)
{
ParameterCheck.mandatory("InputMap", inputMap);
inputMap.put("urls", new ScriptUrls(sysAdminParams));
}
/**
* @see org.alfresco.service.cmr.repository.ScriptService#buildDefaultModel(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
*/
public Map<String, Object> buildDefaultModel(
NodeRef person,
NodeRef companyHome,
NodeRef userHome,
NodeRef script,
NodeRef document,
NodeRef space)
{
Map<String, Object> model = new HashMap<String, Object>();
buildCoreModel(model);
// add the well known node wrapper objects
model.put("companyhome", companyHome);
if (userHome!= null)
{
model.put("userhome", userHome);
}
if (person != null)
{
model.put("person", person);
}
if (script != null)
{
model.put("script", script);
}
if (document != null)
{
model.put("document", document);
}
if (space != null)
{
model.put("space", space);
}
return model;
}
}