Search in sources :

Example 1 with ArgumentHelper

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);
    }
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) PersonService(org.alfresco.service.cmr.security.PersonService) NodeService(org.alfresco.service.cmr.repository.NodeService) ArgumentHelper(org.alfresco.util.ArgumentHelper) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ApplicationContext(org.springframework.context.ApplicationContext) PropertyMap(org.alfresco.util.PropertyMap) ServiceRegistry(org.alfresco.service.ServiceRegistry) AuthenticationService(org.alfresco.service.cmr.security.AuthenticationService)

Example 2 with ArgumentHelper

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();
    }
}
Also used : RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) ArrayList(java.util.ArrayList) ArgumentHelper(org.alfresco.util.ArgumentHelper) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) MutableAuthenticationService(org.alfresco.service.cmr.security.MutableAuthenticationService) PermissionService(org.alfresco.service.cmr.security.PermissionService) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) SearchService(org.alfresco.service.cmr.search.SearchService) ResultSet(org.alfresco.service.cmr.search.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) StoreRef(org.alfresco.service.cmr.repository.StoreRef) TransactionService(org.alfresco.service.transaction.TransactionService) NodeService(org.alfresco.service.cmr.repository.NodeService) M2Model(org.alfresco.repo.dictionary.M2Model) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) DictionaryDAO(org.alfresco.repo.dictionary.DictionaryDAO) RetryingTransactionCallback(org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ServiceRegistry(org.alfresco.service.ServiceRegistry)

Aggregations

ServiceRegistry (org.alfresco.service.ServiceRegistry)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 NodeService (org.alfresco.service.cmr.repository.NodeService)2 TransactionService (org.alfresco.service.transaction.TransactionService)2 ArgumentHelper (org.alfresco.util.ArgumentHelper)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 DictionaryDAO (org.alfresco.repo.dictionary.DictionaryDAO)1 M2Model (org.alfresco.repo.dictionary.M2Model)1 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)1 FileFolderService (org.alfresco.service.cmr.model.FileFolderService)1 FileInfo (org.alfresco.service.cmr.model.FileInfo)1 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)1 StoreRef (org.alfresco.service.cmr.repository.StoreRef)1 ResultSet (org.alfresco.service.cmr.search.ResultSet)1 SearchService (org.alfresco.service.cmr.search.SearchService)1 AuthenticationService (org.alfresco.service.cmr.security.AuthenticationService)1