KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > example > SimpleExample


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.example;
18
19 import javax.jcr.Node;
20 import javax.jcr.NodeIterator;
21 import javax.jcr.Property;
22 import javax.jcr.PropertyIterator;
23 import javax.jcr.Repository;
24 import javax.jcr.Session;
25 import javax.jcr.SimpleCredentials;
26
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.support.ClassPathXmlApplicationContext;
29
30
31
32 /**
33  * Simple Example that demonstrate login and retrieval of top-level Spaces
34  * under Company Home.
35  *
36  * @author David Caruana
37  */

38 public class SimpleExample
39 {
40
41     public static void main(String JavaDoc[] args)
42         throws Exception JavaDoc
43     {
44         // Setup Spring and Transaction Service
45
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alfresco/jcr-context.xml");
46         
47         // Retrieve Repository
48
Repository repository = (Repository)context.getBean("JCR.Repository");
49
50         // Login to workspace
51
// Note: Default workspace is the one used by Alfresco Web Client which contains all the Spaces
52
// and their documents
53
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
54
55         try
56         {
57             // Retrieve Company Home
58
Node root = session.getRootNode();
59             Node companyHome = root.getNode("app:company_home");
60             
61             // Iterator through children of Company Home
62
NodeIterator iterator = companyHome.getNodes();
63             while(iterator.hasNext())
64             {
65                 Node child = iterator.nextNode();
66                 System.out.println(child.getName());
67
68                 PropertyIterator propIterator = child.getProperties();
69                 while(propIterator.hasNext())
70                 {
71                     Property prop = propIterator.nextProperty();
72                     if (!prop.getDefinition().isMultiple())
73                     {
74                         System.out.println(" " + prop.getName() + " = " + prop.getString());
75                     }
76                 }
77             }
78         }
79         finally
80         {
81             session.logout();
82             System.exit(0);
83         }
84         
85     }
86     
87 }
88
Popular Tags