Updated Workflow Interpreter to extend from BaseInterpreter, in preparation for other console interpreters such as WebClientConfig admin console.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@6662 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2007-08-31 14:55:24 +00:00
parent 69637c7013
commit 40b3a8b154
3 changed files with 245 additions and 102 deletions

View File

@@ -0,0 +1,226 @@
/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* As a special exception to the terms and conditions of version 2.0 of
* the GPL, you may redistribute this Program in connection with Free/Libre
* and Open Source Software ("FLOSS") applications as described in Alfresco's
* FLOSS exception. You should have recieved a copy of the text describing
* the FLOSS exception, and it is also available here:
* http://www.alfresco.com/legal/licensing"
*/
package org.alfresco.repo.admin;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.AbstractLifecycleBean;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
/**
* An interactive console
*
*/
public abstract class BaseInterpreter extends AbstractLifecycleBean
{
// dependencies
protected TransactionService transactionService;
protected TenantService tenantService;
/**4
* The reader for interaction.
*/
private BufferedReader fIn;
/**
* Current context
*/
private String username = null;
protected final static String DEFAULT_ADMIN = "admin";
/**
* Last command issued
*/
protected String lastCommand = null;
/**
* Main entry point.
*/
public static void main(String[] args)
{
ApplicationContext context = ApplicationContextHelper.getApplicationContext();
BaseInterpreter console = getConsoleBean(context);
console.username = DEFAULT_ADMIN;
console.rep();
System.exit(0);
}
public static BaseInterpreter getConsoleBean(ApplicationContext context)
{
return null;
}
/**
* Make up a new console.
*/
public BaseInterpreter()
{
fIn = new BufferedReader(new InputStreamReader(System.in));
}
public void setTransactionService(TransactionService transactionService)
{
this.transactionService = transactionService;
}
public void setTenantService(TenantService tenantService)
{
this.tenantService = tenantService;
}
/**
* A Read-Eval-Print loop.
*/
public void rep()
{
// accept commands
while (true)
{
System.out.print("ok> ");
try
{
// get command
final String line = fIn.readLine();
if (line.equals("exit") || line.equals("quit"))
{
return;
}
// execute command in context of currently selected user
long startms = System.currentTimeMillis();
System.out.print(interpretCommand(line));
System.out.println("" + (System.currentTimeMillis() - startms) + "ms");
}
catch (Throwable t)
{
t.printStackTrace(System.err);
System.out.println("");
}
}
}
/**
* Interpret a single command using the BufferedReader passed in for any data needed.
*
* @param line The unparsed command
* @return The textual output of the command.
*/
public String interpretCommand(final String line)
throws IOException
{
String currentUserName = getCurrentUserName();
if (hasAuthority(currentUserName))
{
// execute command in context of currently selected user
return AuthenticationUtil.runAs(new RunAsWork<String>()
{
public String doWork() throws Exception
{
RetryingTransactionCallback<String> txnWork = new RetryingTransactionCallback<String>()
{
public String execute() throws Exception
{
return executeCommand(line);
}
};
return transactionService.getRetryingTransactionHelper().doInTransaction(txnWork);
}
}, currentUserName);
}
else
{
return("Error: User '"+ currentUserName + "' not authorised");
}
}
protected boolean hasAuthority(String username)
{
return ((username != null) && (tenantService.getBaseNameUser(username).equals(DEFAULT_ADMIN)));
}
/**
* Execute a single command using the BufferedReader passed in for any data needed.
*
* TODO: Use decent parser!
*
* @param line The unparsed command
* @return The textual output of the command.
*/
protected abstract String executeCommand(String line) throws IOException;
/**
* Get current user name
*
* @return user name
*/
public String getCurrentUserName()
{
if (username == null)
{
return AuthenticationUtil.getCurrentUserName();
}
return username;
}
public void setCurrentUserName(String username)
{
this.username = username;
}
/* (non-Javadoc)
* @see org.alfresco.util.AbstractLifecycleBean#onBootstrap(org.springframework.context.ApplicationEvent)
*/
@Override
protected void onBootstrap(ApplicationEvent event)
{
// NOOP
}
/* (non-Javadoc)
* @see org.alfresco.util.AbstractLifecycleBean#onShutdown(org.springframework.context.ApplicationEvent)
*/
@Override
protected void onShutdown(ApplicationEvent event)
{
// NOOP
}
}

View File

@@ -50,7 +50,7 @@ import org.alfresco.service.namespace.NamespaceService;
* *
* Example store URLs * Example store URLs
* <code>workspace://SpacesStore/${spaces.company_home.childname}/${spaces.dictionary.childname}/${spaces.webclient_extension.childname}/cm:web-client-config-custom.xml</code> * <code>workspace://SpacesStore/${spaces.company_home.childname}/${spaces.dictionary.childname}/${spaces.webclient_extension.childname}/cm:web-client-config-custom.xml</code>
* <code>workspace://SpacesStore/app:company_home/app:dictionary/cm:webclient_extension/cm:web-client-config-custom.xml</code> * <code>workspace://SpacesStore/app:company_home/app:dictionary/app:webclient_extension/cm:web-client-config-custom.xml</code>
*/ */
public class RepoUrlConfigSource extends UrlConfigSource public class RepoUrlConfigSource extends UrlConfigSource
{ {

View File

@@ -24,11 +24,9 @@
*/ */
package org.alfresco.repo.workflow; package org.alfresco.repo.workflow;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
@@ -38,12 +36,11 @@ import java.util.Map;
import org.alfresco.i18n.I18NUtil; import org.alfresco.i18n.I18NUtil;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.admin.BaseInterpreter;
import org.alfresco.repo.avm.AVMNodeConverter; import org.alfresco.repo.avm.AVMNodeConverter;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.security.authority.AuthorityDAO; import org.alfresco.repo.security.authority.AuthorityDAO;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.cmr.avm.AVMNodeDescriptor; import org.alfresco.service.cmr.avm.AVMNodeDescriptor;
import org.alfresco.service.cmr.avm.AVMService; import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.avmsync.AVMDifference; import org.alfresco.service.cmr.avmsync.AVMDifference;
@@ -68,8 +65,6 @@ import org.alfresco.service.cmr.workflow.WorkflowTransition;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService; import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.AbstractLifecycleBean;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.GUID; import org.alfresco.util.GUID;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
@@ -80,13 +75,12 @@ import org.springframework.core.io.ClassPathResource;
* *
* @author davidc * @author davidc
*/ */
public class WorkflowInterpreter extends AbstractLifecycleBean public class WorkflowInterpreter extends BaseInterpreter
{ {
// Service dependencies // Service dependencies
private WorkflowService workflowService; private WorkflowService workflowService;
private NamespaceService namespaceService; private NamespaceService namespaceService;
private NodeService nodeService; private NodeService nodeService;
private TransactionService transactionService;
private AuthorityDAO authorityDAO; private AuthorityDAO authorityDAO;
private AVMService avmService; private AVMService avmService;
private AVMSyncService avmSyncService; private AVMSyncService avmSyncService;
@@ -94,23 +88,13 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
private FileFolderService fileFolderService; private FileFolderService fileFolderService;
/**
* The reader for interaction.
*/
private BufferedReader fIn;
/** /**
* Current context * Current context
*/ */
private WorkflowDefinition currentWorkflowDef = null; private WorkflowDefinition currentWorkflowDef = null;
private WorkflowPath currentPath = null; private WorkflowPath currentPath = null;
private String currentDeploy = null; private String currentDeploy = null;
private String username = "admin";
/**
* Last command issued
*/
private String lastCommand = null;
/** /**
* Variables * Variables
@@ -118,25 +102,7 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
private Map<QName, Serializable> vars = new HashMap<QName, Serializable>(); private Map<QName, Serializable> vars = new HashMap<QName, Serializable>();
/**
* Main entry point.
*/
public static void main(String[] args)
throws IOException
{
ApplicationContext context = ApplicationContextHelper.getApplicationContext();
WorkflowInterpreter console = (WorkflowInterpreter)context.getBean("workflowInterpreter");
console.rep();
System.exit(0);
}
/**
* Make up a new console.
*/
public WorkflowInterpreter()
{
fIn = new BufferedReader(new InputStreamReader(System.in));
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.alfresco.util.AbstractLifecycleBean#onBootstrap(org.springframework.context.ApplicationEvent) * @see org.alfresco.util.AbstractLifecycleBean#onBootstrap(org.springframework.context.ApplicationEvent)
@@ -146,8 +112,12 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
{ {
try try
{ {
setCurrentUserName(BaseInterpreter.DEFAULT_ADMIN);
interpretCommand("var bpm:package package 1"); interpretCommand("var bpm:package package 1");
interpretCommand("var bpm:assignee person admin"); interpretCommand("var bpm:assignee person admin");
setCurrentUserName(null);
} }
catch(IOException e) catch(IOException e)
{ {
@@ -237,60 +207,17 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
} }
/** /**
* A Read-Eval-Print loop. *
*/ */
public void rep() public static BaseInterpreter getConsoleBean(ApplicationContext context)
{ {
// accept commands return (WorkflowInterpreter)context.getBean("workflowInterpreter");
while (true)
{
System.out.print("ok> ");
try
{
// get command
final String line = fIn.readLine();
if (line.equals("exit") || line.equals("quit"))
{
return;
}
// execute command in context of currently selected user
long startms = System.currentTimeMillis();
System.out.print(interpretCommand(line));
System.out.println("" + (System.currentTimeMillis() - startms) + "ms");
}
catch (Exception e)
{
e.printStackTrace(System.err);
System.out.println("");
}
}
} }
/** protected boolean hasAuthority(String username)
* Interpret a single command using the BufferedReader passed in for any data needed.
*
* @param line The unparsed command
* @return The textual output of the command.
*/
public String interpretCommand(final String line)
throws IOException
{ {
// execute command in context of currently selected user // admin can change to any user (via worklow command "user <username>")
return AuthenticationUtil.runAs(new RunAsWork<String>() return true;
{
public String doWork() throws Exception
{
RetryingTransactionCallback<String> txnWork = new RetryingTransactionCallback<String>()
{
public String execute() throws Exception
{
return executeCommand(line);
}
};
return transactionService.getRetryingTransactionHelper().doInTransaction(txnWork);
}
}, username);
} }
/** /**
@@ -301,7 +228,7 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
* @param line The unparsed command * @param line The unparsed command
* @return The textual output of the command. * @return The textual output of the command.
*/ */
private String executeCommand(String line) protected String executeCommand(String line)
throws IOException throws IOException
{ {
String[] command = line.split(" "); String[] command = line.split(" ");
@@ -911,9 +838,9 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
{ {
if (command.length == 2) if (command.length == 2)
{ {
username = command[1]; setCurrentUserName(command[1]);
} }
out.println("using user " + username); out.println("using user " + getCurrentUserName());
} }
else if (command[0].equals("start")) else if (command[0].equals("start"))
@@ -1316,14 +1243,4 @@ public class WorkflowInterpreter extends AbstractLifecycleBean
return currentWorkflowDef; return currentWorkflowDef;
} }
/**
* Get current user name
*
* @return user name
*/
public String getCurrentUserName()
{
return username;
}
} }