use of org.alfresco.util.ArgumentHelper in project alfresco-repository by Alfresco.
the class PersonServiceLoader method main.
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("\n").append("Usage\n").append(" PersonServiceLoader --user=<username> --pwd=<password> --batch-count=<batch-count> --batch-size=<batch-size> --threads=<threads>\n");
String usage = sb.toString();
ArgumentHelper argHelper = new ArgumentHelper(usage, args);
try {
String user = argHelper.getStringValue("user", true, true);
String pwd = argHelper.getStringValue("pwd", true, true);
int batchCount = argHelper.getIntegerValue("batch-count", true, 1, Integer.MAX_VALUE);
int batchSize = argHelper.getIntegerValue("batch-size", true, 1, Integer.MAX_VALUE);
int threads = argHelper.getIntegerValue("threads", true, 1, Integer.MAX_VALUE);
ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
// Create the worker instance
PersonServiceLoader loader = new PersonServiceLoader(ctx, batchSize, batchCount);
loader.run(user, pwd, threads);
// check the lazy creation
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
final AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
final PersonService personService = serviceRegistry.getPersonService();
final TransactionService transactionService = serviceRegistry.getTransactionService();
final NodeService nodeService = serviceRegistry.getNodeService();
String firstName = "" + System.currentTimeMillis();
String lastName = String.format("%05d", -1);
final String username = GUID.generate();
String emailAddress = String.format("%s.%s@xyz.com", firstName, lastName);
PropertyMap properties = new PropertyMap(7);
properties.put(ContentModel.PROP_USERNAME, username);
properties.put(ContentModel.PROP_FIRSTNAME, firstName);
properties.put(ContentModel.PROP_LASTNAME, lastName);
properties.put(ContentModel.PROP_EMAIL, emailAddress);
NodeRef madePerson = personService.createPerson(properties);
NodeRef homeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(madePerson, ContentModel.PROP_HOMEFOLDER));
if (homeFolder != null) {
throw new IllegalStateException("Home folder created eagerly");
}
RetryingTransactionHelper helper = transactionService.getRetryingTransactionHelper();
helper.doInTransaction(new RetryingTransactionCallback<Void>() {
public Void execute() throws Throwable {
NodeRef person = personService.getPerson(username);
NodeRef homeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(person, ContentModel.PROP_HOMEFOLDER));
if (homeFolder == null) {
throw new IllegalStateException("Home folder not created lazily");
}
return null;
}
}, true, true);
NodeRef autoPerson = personService.getPerson(GUID.generate());
NodeRef autoHomeFolder = DefaultTypeConverter.INSTANCE.convert(NodeRef.class, nodeService.getProperty(autoPerson, ContentModel.PROP_HOMEFOLDER));
if (autoHomeFolder == null) {
throw new IllegalStateException("Home folder not created lazily for auto created users");
}
// All done
ApplicationContextHelper.closeApplicationContext();
System.exit(0);
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
argHelper.printUsage();
System.exit(1);
} catch (Throwable e) {
logger.error("PersonServiceLoader (userCount, batchSize) failed.", e);
System.exit(1);
}
}
use of org.alfresco.util.ArgumentHelper in project alfresco-repository by Alfresco.
the class FileFolderPerformanceTester method run.
private static void run(final ApplicationContext ctx, String... args) throws Throwable {
ArgumentHelper argHelper = new ArgumentHelper(getUsage(), args);
final int fileCount = argHelper.getIntegerValue("files", true, 1, 10000);
final String folderRefStr = argHelper.getStringValue("folder", false, true);
final int threadCount = argHelper.getIntegerValue("threads", false, 1, 100);
final NodeRef selectedFolderNodeRef = folderRefStr == null ? null : new NodeRef(folderRefStr);
ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
final MutableAuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
final PermissionService permissionService = serviceRegistry.getPermissionService();
final NodeService nodeService = serviceRegistry.getNodeService();
final SearchService searchService = serviceRegistry.getSearchService();
final TransactionService transactionService = serviceRegistry.getTransactionService();
final FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
RunAsWork<String> createUserRunAs = new RunAsWork<String>() {
public String doWork() throws Exception {
String user = GUID.generate();
authenticationService.createAuthentication(user, user.toCharArray());
return user;
}
};
final String user = AuthenticationUtil.runAs(createUserRunAs, AuthenticationUtil.getSystemUserName());
// Create the files
final RetryingTransactionCallback<NodeRef> createCallback = new RetryingTransactionCallback<NodeRef>() {
public NodeRef execute() throws Throwable {
AuthenticationUtil.pushAuthentication();
DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO");
M2Model model = M2Model.createModel("tempModel");
model.createNamespace("test", "t");
model.createNamespace("testx", "");
for (int m = 0; m < 30; m++) {
model.createAspect("t:aspect_" + m);
}
dictionaryDao.putModel(model);
NodeRef folderNodeRef = null;
try {
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
if (selectedFolderNodeRef == null) {
// find the guest folder
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
ResultSet rs = searchService.query(storeRef, SearchService.LANGUAGE_XPATH, "/app:company_home");
try {
if (rs.length() == 0) {
throw new AlfrescoRuntimeException("Didn't find Company Home");
}
NodeRef companyHomeNodeRef = rs.getNodeRef(0);
folderNodeRef = fileFolderService.create(companyHomeNodeRef, "TOP_FOLDER_" + System.currentTimeMillis(), ContentModel.TYPE_FOLDER).getNodeRef();
System.out.println("Created folder " + folderNodeRef + " with user " + user);
} finally {
rs.close();
}
// Grant permissions
permissionService.setPermission(folderNodeRef, user, PermissionService.ALL_PERMISSIONS, true);
} else {
folderNodeRef = selectedFolderNodeRef;
// Grant permissions
permissionService.setPermission(folderNodeRef, user, PermissionService.ALL_PERMISSIONS, true);
System.out.println("Reusing folder " + folderNodeRef + " with user " + user);
}
} finally {
AuthenticationUtil.popAuthentication();
}
if (selectedFolderNodeRef == null) {
List<String> largeCollection = new ArrayList<String>(1000);
for (int i = 0; i < 50; i++) {
largeCollection.add(String.format("Large-collection-value-%05d", i));
}
// Create the files
for (int i = 0; i < fileCount; i++) {
FileInfo fileInfo = fileFolderService.create(folderNodeRef, String.format("FILE-%4d", i), ContentModel.TYPE_CONTENT);
NodeRef nodeRef = fileInfo.getNodeRef();
nodeService.setProperty(nodeRef, QName.createQName("{test}mv"), (Serializable) largeCollection);
for (int m = 0; m < 30; m++) {
nodeService.addAspect(nodeRef, QName.createQName("{test}aspect_" + m), null);
}
// write the content
ContentWriter writer = fileFolderService.getWriter(nodeRef);
writer.setEncoding("UTF-8");
writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
writer.putContent("Some small text data");
}
System.out.println("Created " + fileCount + " files in folder " + folderNodeRef);
}
// Done
return folderNodeRef;
}
};
RunAsWork<NodeRef> createRunAs = new RunAsWork<NodeRef>() {
public NodeRef doWork() throws Exception {
return transactionService.getRetryingTransactionHelper().doInTransaction(createCallback);
}
};
final NodeRef folderNodeRef = AuthenticationUtil.runAs(createRunAs, user);
// Now wait for some input before commencing the read run
System.out.print("Hit any key to commence directory listing ...");
System.in.read();
final RunAsWork<List<FileInfo>> readRunAs = new RunAsWork<List<FileInfo>>() {
public List<FileInfo> doWork() throws Exception {
return fileFolderService.list(folderNodeRef);
}
};
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
Thread readThread = new Thread("FolderList-" + i) {
int iteration = 0;
public void run() {
while (++iteration <= 2) {
runImpl();
}
}
private void runImpl() {
String threadName = Thread.currentThread().getName();
long start = System.currentTimeMillis();
List<FileInfo> nodeRefs = AuthenticationUtil.runAs(readRunAs, user);
long time = System.currentTimeMillis() - start;
double average = (double) time / (double) (fileCount);
// Make sure that we have the correct number of entries
if (folderRefStr != null && nodeRefs.size() != fileCount) {
System.err.println("WARNING: Thread " + threadName + " got " + nodeRefs.size() + " but expected " + fileCount);
}
System.out.print("\n" + "Thread " + threadName + ": \n" + " Read " + String.format("%4d", fileCount) + " files \n" + " Average: " + String.format("%10.2f", average) + " ms per file \n" + " Average: " + String.format("%10.2f", 1000.0 / average) + " files per second");
}
};
readThread.start();
threads[i] = readThread;
}
for (int i = 0; i < threads.length; i++) {
threads[i].join();
}
}
Aggregations