KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > demo > kernel > CodeClient


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: CodeClient.java,v 1.3 2005/10/27 18:02:29 wfro Exp $
5  * Description: openCRX code access client
6  * Revision: $Revision: 1.3 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2005/10/27 18:02:29 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.demo.kernel;
57
58 import java.util.Collection JavaDoc;
59 import java.util.Iterator JavaDoc;
60
61 import javax.naming.Context JavaDoc;
62 import javax.naming.InitialContext JavaDoc;
63 import javax.naming.NamingException JavaDoc;
64
65 import org.opencrx.kernel.client.ClientHelper;
66 import org.opencrx.kernel.code1.cci.CodeValueContainer;
67 import org.opencrx.kernel.code1.cci.CodeValueContainerFilter;
68 import org.opencrx.kernel.code1.cci.CodeValueEntry;
69 import org.openmdx.base.accessor.generic.cci.ObjectFactory_1_0;
70 import org.openmdx.base.accessor.jmi.cci.JmiServiceException;
71 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0;
72 import org.openmdx.base.accessor.jmi.spi.RefRootPackage_1;
73 import org.openmdx.base.exception.ServiceException;
74 import org.openmdx.compatibility.base.naming.Path;
75 import org.openmdx.compatibility.base.query.FilterOperators;
76
77 public class CodeClient {
78
79     //-----------------------------------------------------------------------
80
public static void main(
81         String JavaDoc[] args
82     ) {
83         try {
84             Context JavaDoc componentEnvironment = (Context JavaDoc)new InitialContext JavaDoc().lookup("java:comp/env");
85             
86             // Get openMDX object factory
87
ObjectFactory_1_0 of = ClientHelper.createObjectFactory(
88                 componentEnvironment,
89                 "openmdx/data"
90             );
91             
92             // Get JMI root package. For more information about
93
// 'Programming a JMI client for openMDX' see
94
// http://www.openmdx.org/openmdx/1.3/example-lab/htmlsingle/Example-Lab.html
95
RefPackage_1_0 rootPkg = new RefRootPackage_1(
96                 of,
97                 null, // impls
98
null, // context
99
"cci",
100                 false // throw NOT_FOUND
101
);
102
103             // Load models required by client
104
ClientHelper.loadModels(
105                 componentEnvironment,
106                 rootPkg
107             );
108
109             // get code JMI package
110
org.opencrx.kernel.code1.cci.code1Package code1Pkg =
111                 (org.opencrx.kernel.code1.cci.code1Package)rootPkg.refPackage(
112                     org.opencrx.kernel.code1.cci.code1Package.class.getName()
113                 );
114             
115             // Get provider and segment name
116
String JavaDoc providerName = (String JavaDoc)componentEnvironment.lookup("providerName");
117             
118             // get code segment
119
org.opencrx.kernel.code1.cci.Segment codeSegment =
120                 (org.opencrx.kernel.code1.cci.Segment)rootPkg.refObject(
121                     "xri:@openmdx:org.opencrx.kernel.code1/provider/" + providerName + "/segment/Root"
122                 );
123             
124             // get parameters
125
String JavaDoc name = (String JavaDoc)componentEnvironment.lookup("name");
126             
127             // find code value container matching the given name
128
System.out.println("openCRX CodeAccessor: Find codes of field " + name);
129
130             // find code value container
131
CodeValueContainerFilter filter = code1Pkg.createCodeValueContainerFilter();
132             filter.thereExistsName(
133                 FilterOperators.IS_IN,
134                 new String JavaDoc[]{name}
135             );
136             Collection JavaDoc codeValueContainers = codeSegment.getValueContainer(filter);
137             if(codeValueContainers.size() == 0) {
138                 System.out.println("no code value container found matching name " + name);
139             }
140             else {
141                 for(
142                     Iterator JavaDoc i = codeValueContainers.iterator();
143                     i.hasNext();
144                 ) {
145                     CodeValueContainer codeValueContainer = (CodeValueContainer)i.next();
146                     System.out.println("code value container=" + codeValueContainer.getName());
147                     for(
148                         Iterator JavaDoc j = codeValueContainer.getEntry().iterator();
149                         j.hasNext();
150                     ) {
151                         CodeValueEntry entry = (CodeValueEntry)j.next();
152                         System.out.println("code=" + new Path(entry.refMofId()).getBase());
153                         // shortText
154
System.out.println(" shortText");
155                         int kk = 0;
156                         for(
157                             Iterator JavaDoc k = entry.getShortText().iterator();
158                             k.hasNext();
159                             kk++
160                         ) {
161                             System.out.println(" shortText[" + kk + "]=" + k.next());
162                         }
163                         // longText
164
System.out.println(" longText");
165                         kk = 0;
166                         for(
167                             Iterator JavaDoc k = entry.getLongText().iterator();
168                             k.hasNext();
169                             kk++
170                         ) {
171                             System.out.println(" longText[" + kk + "]=" + k.next());
172                         }
173                     }
174                 }
175             }
176         }
177         catch (NamingException JavaDoc e) {
178             e.printStackTrace();
179         }
180         catch (ServiceException e) {
181             e.printStackTrace();
182         }
183         catch (JmiServiceException e) {
184             e.printStackTrace();
185         }
186     }
187
188 }
189
190 //--- End of File -----------------------------------------------------------
191
Popular Tags