KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > test > TestData


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.test;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.alfresco.repo.importer.ImporterBootstrap;
24 import org.alfresco.repo.security.authentication.AuthenticationComponent;
25 import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
26 import org.alfresco.service.ServiceRegistry;
27 import org.alfresco.service.cmr.repository.NodeService;
28 import org.alfresco.service.cmr.repository.StoreRef;
29 import org.alfresco.service.cmr.security.PermissionService;
30 import org.alfresco.service.cmr.view.ImporterService;
31 import org.alfresco.service.namespace.NamespaceService;
32 import org.alfresco.service.transaction.TransactionService;
33 import org.springframework.context.ApplicationContext;
34 import org.springframework.context.support.ClassPathXmlApplicationContext;
35
36
37
38 public class TestData
39 {
40     public static final String JavaDoc TEST_WORKSPACE = "test";
41
42     /**
43      * Generate Test Workspace within Repository
44      *
45      * @param args
46      */

47     public static void main(String JavaDoc[] args)
48     {
49         ApplicationContext context = new ClassPathXmlApplicationContext("org/alfresco/jcr/test/test-context.xml");
50         generateTestData(context, TEST_WORKSPACE);
51         System.out.println("Generated TCK test data to workspace: " + TEST_WORKSPACE);
52         System.exit(0);
53     }
54
55     /**
56      * Bootstrap Repository with JCR Test Data
57      *
58      * @param applicationContext
59      * @param workspaceName
60      */

61     public static void generateTestData(ApplicationContext applicationContext, String JavaDoc workspaceName)
62     {
63         {
64             // Bootstrap Users
65
MutableAuthenticationDao authDAO = (MutableAuthenticationDao) applicationContext.getBean("alfDaoImpl");
66             if (authDAO.userExists("superuser") == false)
67             {
68                 authDAO.createUser("superuser", "".toCharArray());
69             }
70             if (authDAO.userExists("user") == false)
71             {
72                 authDAO.createUser("user", "".toCharArray());
73             }
74             if (authDAO.userExists("anonymous") == false)
75             {
76                 authDAO.createUser("anonymous", "".toCharArray());
77             }
78         }
79
80         try
81         {
82             AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
83             authenticationComponent.setSystemUserAsCurrentUser();
84
85             try
86             {
87                 // Bootstrap Workspace Test Data
88
StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, workspaceName);
89
90                 ImporterBootstrap bootstrap = new ImporterBootstrap();
91                 bootstrap.setAuthenticationComponent((AuthenticationComponent) applicationContext.getBean("authenticationComponent"));
92                 bootstrap.setImporterService((ImporterService) applicationContext.getBean(ServiceRegistry.IMPORTER_SERVICE.getLocalName()));
93                 bootstrap.setNodeService((NodeService) applicationContext.getBean(ServiceRegistry.NODE_SERVICE.getLocalName()));
94                 bootstrap.setNamespaceService((NamespaceService) applicationContext.getBean(ServiceRegistry.NAMESPACE_SERVICE.getLocalName()));
95                 bootstrap.setTransactionService((TransactionService) applicationContext.getBean(ServiceRegistry.TRANSACTION_SERVICE.getLocalName()));
96                 bootstrap.setStoreUrl(storeRef.toString());
97
98                 List JavaDoc<Properties JavaDoc> views = new ArrayList JavaDoc<Properties JavaDoc>();
99                 Properties JavaDoc testView = new Properties JavaDoc();
100                 testView.setProperty("path", "/");
101                 testView.setProperty("location", "org/alfresco/jcr/test/testData.xml");
102                 views.add(testView);
103                 bootstrap.setBootstrapViews(views);
104                 bootstrap.bootstrap();
105
106                 // Bootstrap clears security context
107
authenticationComponent.setSystemUserAsCurrentUser();
108                 
109                 PermissionService permissionService = (PermissionService)applicationContext.getBean(ServiceRegistry.PERMISSIONS_SERVICE.getLocalName());
110                 NodeService nodeService = (NodeService)applicationContext.getBean(ServiceRegistry.NODE_SERVICE.getLocalName());
111
112 // permissionService.setPermission(nodeService.getRootNode(storeRef), PermissionService.ALL_AUTHORITIES, PermissionService.ALL_PERMISSIONS, true);
113
permissionService.setPermission(nodeService.getRootNode(storeRef), "superuser", PermissionService.ALL_PERMISSIONS, true);
114                 permissionService.setPermission(nodeService.getRootNode(storeRef), "anonymous", PermissionService.READ, true);
115                 permissionService.setPermission(nodeService.getRootNode(storeRef), "user", PermissionService.READ, true);
116                 permissionService.setPermission(nodeService.getRootNode(storeRef), "user", PermissionService.WRITE, true);
117             }
118             finally
119             {
120                 authenticationComponent.clearCurrentSecurityContext();
121             }
122         }
123         catch (RuntimeException JavaDoc e)
124         {
125             System.out.println("Exception: " + e);
126             e.printStackTrace();
127             throw e;
128         }
129     }
130
131 }
132
Popular Tags