KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > ui > logicalview > entres > JMSDestination


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.ejbcore.ui.logicalview.entres;
21
22 import org.netbeans.api.project.Project;
23
24 /**
25  * Provide an encapsulation of a JMS Destination.
26  * @author Chris Webster
27  * @author Martin Adamek
28  */

29 public class JMSDestination {
30
31     private final String JavaDoc destination;
32     private final Project project;
33     private final String JavaDoc type;
34     
35     public JMSDestination(String JavaDoc destination, Project project, String JavaDoc type) {
36         this.destination = destination;
37         this.project = project;
38         this.type = type;
39     }
40     
41     public String JavaDoc toString() {
42         return destination;
43     }
44     
45     public String JavaDoc getDestination() {
46         return destination;
47     }
48     
49     public String JavaDoc getType() {
50         return type;
51     }
52     
53     public Project getProject() {
54         return project;
55     }
56     
57     public String JavaDoc getConnectionFactoryName(){
58         return "jms/" + getDestination() + "Factory"; //NO18N
59
}
60     
61     public String JavaDoc getDestinationName(){
62         return "jms/" + getDestination();//NO18N
63
}
64     
65 // public void genMethods(EnterpriseReferenceContainer container, String className, FileObject fileObject,
66
// final ElementHandle<TypeElement> target, ServiceLocatorStrategy serviceLocatorStrategy) throws IOException {
67
// JavaSource javaSource = JavaSource.forFileObject(fileObject);
68
// final boolean[] isInjectionTarget = new boolean[1];
69
// javaSource.runUserActionTask(new AbstractTask<CompilationController>() {
70
// public void run(CompilationController controller) throws Exception {
71
// TypeElement typeElement = target.resolve(controller);
72
// isInjectionTarget[0] = InjectionTargetQuery.isInjectionTarget(controller, typeElement);
73
// }
74
// }, true);
75
// this.supportsInjection = isInjectionTarget[0];
76
// String destinationFieldName = null;
77
// String connectionFactoryFieldName = null;
78
// String factoryName = null;
79
// String destinationName = null;
80
//
81
// if (supportsInjection){
82
// factoryName = getConnectionFactoryName();
83
// destinationName = getDestinationName();
84
// connectionFactoryFieldName = createInjectedField(fileObject, className, factoryName, "javax.jms.ConnectionFactory"); //NO18N
85
// destinationFieldName = createInjectedField(fileObject, className, destinationName, type);
86
// } else {
87
// factoryName = generateConnectionFactoryReference(container, fileObject, className);
88
// destinationName = generateDestinationReference(container, fileObject, className, project);
89
// }
90
// String sendMethodName = createSendMethod(fileObject, className, getDestination());
91
// createJMSProducer(fileObject, className, factoryName, connectionFactoryFieldName, destinationName,
92
// destinationFieldName,sendMethodName, serviceLocatorStrategy);
93
// }
94
//
95
// private String generateConnectionFactoryReference(EnterpriseReferenceContainer container, FileObject referencingFile, String referencingClass) throws IOException {
96
// ResourceRef ref = container.createResourceRef(referencingClass);
97
// ref.setResRefName(getConnectionFactoryName()); // NOI18N
98
// ref.setResAuth(org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_AUTH_CONTAINER);
99
// ref.setResSharingScope(org.netbeans.modules.j2ee.dd.api.common.ResourceRef.RES_SHARING_SCOPE_SHAREABLE);
100
// ref.setResType("javax.jms.ConnectionFactory"); //NOI18N
101
// return container.addResourceRef(ref, referencingFile, referencingClass);
102
// }
103
//
104
// private String generateDestinationReference(EnterpriseReferenceContainer container, FileObject referencingFile, String referencingClass, Project project) throws IOException {
105
// MessageDestinationRef ref = container.createDestinationRef(referencingClass);
106
// ref.setMessageDestinationUsage(PRODUCES);
107
// ref.setMessageDestinationType(getType());
108
// ref.setMessageDestinationRefName(getDestinationName());
109
// // this may need to generalized later if jms producers are expected
110
// // in web modules
111
// ProjectInformation pi = ProjectUtils.getInformation(getProject());
112
// ref.setMessageDestinationLink(pi.getName()+".jar#"+getDestination());
113
// if (getProject().equals(project)) {
114
// String linkWithoutModule = ref.getMessageDestinationLink();
115
// linkWithoutModule = linkWithoutModule.substring(linkWithoutModule.indexOf('#')+1);
116
// ref.setMessageDestinationLink(linkWithoutModule);
117
// }
118
// return container.addDestinationRef(ref, referencingFile, referencingClass);
119
// }
120
//
121
// /**
122
// * Creates an injected resource field for the given <code>target</code>. The name
123
// * of the field will be derivated from the given <code>mappedName</code>.
124
// * @param target the target class
125
// * @param mappedName the value for resource's mappedName attribute
126
// * @param fieldType the class of the field.
127
// * @return name of the created field.
128
// */
129
// private String createInjectedField(FileObject fileObject, String className, String mappedName, String fieldType) throws IOException {
130
// String fieldName = Utils.jndiNameToCamelCase(mappedName, true, "jms");
131
// _RetoucheUtil.generateAnnotatedField(
132
// fileObject,
133
// className,
134
// "javax.annotation.Resource",
135
// fieldName,
136
// fieldType,
137
// Collections.singletonMap("mappedName", mappedName),
138
// InjectionTargetQuery.isStaticReferenceRequired(fileObject, className)
139
// );
140
// return fieldName;
141
// }
142
//
143
// private String createSendMethod(FileObject fileObject, final String className, String destination) throws IOException {
144
// final MethodModel.Variable[] parameters = new MethodModel.Variable[] {
145
// MethodModel.Variable.create("javax.jms.Session", "session"),
146
// MethodModel.Variable.create(Object.class.getName(), "messageData")
147
// };
148
// String methodName = "createJMSMessageFor" + destination;
149
// final MethodModel methodModel = MethodModel.create(
150
// methodName,
151
// "javax.jms.Message",
152
// "// TODO create and populate message to send\n" +
153
// "// javax.jms.TextMessage tm = session.createTextMessage();\n" +
154
// "// tm.setText(messageData.toString());\n"+
155
// "// return tm;\n",
156
// Arrays.asList(parameters),
157
// Collections.singletonList("javax.jms.JMSException"),
158
// Collections.singleton(Modifier.PRIVATE)
159
// );
160
// JavaSource javaSource = JavaSource.forFileObject(fileObject);
161
// javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
162
// public void run(WorkingCopy workingCopy) throws IOException {
163
// workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
164
// TypeElement typeElement = workingCopy.getElements().getTypeElement(className);
165
// MethodTree methodTree = MethodModelSupport.createMethodTree(workingCopy, methodModel);
166
// ClassTree classTree = workingCopy.getTrees().getTree(typeElement);
167
// ClassTree newClassTree = workingCopy.getTreeMaker().addClassMember(classTree, methodTree);
168
// workingCopy.rewrite(classTree, newClassTree);
169
// }
170
// }).commit();
171
// return methodName;
172
// }
173
//
174
// private void createJMSProducer(
175
// FileObject fileObject,
176
// final String className,
177
// String connectionFactoryName,
178
// String connectionFactoryFieldName,
179
// String destinationName,
180
// String destinationFieldName,
181
// String sendMethodName,
182
// ServiceLocatorStrategy slStrategy) throws IOException {
183
// String destName = destinationName.substring(destinationName.lastIndexOf('/') + 1);
184
// StringBuffer destBuff = new StringBuffer(destName);
185
// destBuff.setCharAt(0, Character.toUpperCase(destBuff.charAt(0)));
186
//
187
// String body = null;
188
// if (supportsInjection){
189
// body = getSendJMSCodeWithInjectedFields(connectionFactoryFieldName, destinationFieldName, sendMethodName);
190
// } else if (slStrategy == null) {
191
// body = getSendJMSCode(connectionFactoryName, destinationName, sendMethodName);
192
// } else {
193
// body = getSendJMSCode(connectionFactoryName, destinationName, sendMethodName, slStrategy, fileObject, className);
194
// }
195
//
196
// final MethodModel methodModel = MethodModel.create(
197
// "sendJMSMessageTo" + destBuff,
198
// "void",
199
// body,
200
// Collections.singletonList(MethodModel.Variable.create(Object.class.getName(), "messageData")),
201
// Collections.singletonList("javax.jms.JMSException"),
202
// Collections.singleton(Modifier.PRIVATE)
203
// );
204
// JavaSource javaSource = JavaSource.forFileObject(fileObject);
205
// javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
206
// public void run(WorkingCopy workingCopy) throws IOException {
207
// workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
208
// TypeElement typeElement = workingCopy.getElements().getTypeElement(className);
209
// MethodTree methodTree = MethodModelSupport.createMethodTree(workingCopy, methodModel);
210
// ClassTree classTree = workingCopy.getTrees().getTree(typeElement);
211
// ClassTree newClassTree = workingCopy.getTreeMaker().addClassMember(classTree, methodTree);
212
// workingCopy.rewrite(classTree, newClassTree);
213
// }
214
// }).commit();
215
// }
216
//
217
// /**
218
// * @return String representing the code for send jms method using injected
219
// * fields.
220
// */
221
// private String getSendJMSCodeWithInjectedFields(String connectionFactoryFieldName,
222
// String destinationFieldName,
223
// String messageMethodName){
224
//
225
// return MessageFormat.format(
226
// "javax.jms.Connection connection = null;\n" +
227
// "javax.jms.Session session = null;\n" +
228
// "try '{' \n" +
229
// "connection = {0}.createConnection();\n" +
230
// "session = connection.createSession(false,javax.jms.Session.AUTO_ACKNOWLEDGE);\n" +
231
// "javax.jms.MessageProducer messageProducer = session.createProducer({1});\n" +
232
// "messageProducer.send({2}(session, messageData));\n" +
233
// " '}' finally '{'\n" +
234
// "if (session != null) '{'\n"+
235
// " session.close();\n" +
236
// "'}'\n" +
237
// "if (connection != null) '{'\n" +
238
// "connection.close();\n" +
239
// "'}'\n" +
240
// "'}'\n",
241
// connectionFactoryFieldName, destinationFieldName, messageMethodName);
242
// }
243
//
244
// private String getSendJMSCode(String connectionName, String destinationName,
245
// String messageMethodName, ServiceLocatorStrategy sls,
246
// FileObject fileObject, String className) {
247
// String connectionFactory = sls.genJMSFactory(connectionName, fileObject, className);
248
// String destination = sls.genDestinationLookup(destinationName, fileObject, className);
249
// return MessageFormat.format(
250
// "javax.jms.ConnectionFactory cf = (javax.jms.ConnectionFactory) " + connectionFactory + ";\n" +
251
// "javax.jms.Connection conn = null;\n" +
252
// "javax.jms.Session s = null;\n" +
253
// "try '{' \n" +
254
// "conn = cf.createConnection();\n" +
255
// "s = conn.createSession(false,s.AUTO_ACKNOWLEDGE);\n" +
256
// "javax.jms.Destination destination = (javax.jms.Destination) " + destination + ";\n" +
257
// "javax.jms.MessageProducer mp = s.createProducer(destination);\n" +
258
// "mp.send({2}(s,messageData));\n" +
259
// " '}' finally '{'\n" +
260
// "if (s != null) '{'\n"+
261
// " s.close();\n" +
262
// "'}'\n" +
263
// "if (conn != null) '{'\n" +
264
// "conn.close();\n" +
265
// "'}'\n" +
266
// "'}'\n",
267
// new Object[] {connectionName, destinationName, messageMethodName});
268
// }
269
//
270
// private String getSendJMSCode(String connectionName, String destinationName,
271
// String messageMethodName) {
272
// return MessageFormat.format(
273
// "javax.naming.Context c = new javax.naming.InitialContext();\n" +
274
// "javax.jms.ConnectionFactory cf = (javax.jms.ConnectionFactory) c.lookup(\"java:comp/env/{0}\");\n" +
275
// "javax.jms.Connection conn = null;\n" +
276
// "javax.jms.Session s = null;\n" +
277
// "try '{' \n" +
278
// "conn = cf.createConnection();\n" +
279
// "s = conn.createSession(false,s.AUTO_ACKNOWLEDGE);\n" +
280
// "javax.jms.Destination destination = (javax.jms.Destination) c.lookup(\"java:comp/env/{1}\");\n" +
281
// "javax.jms.MessageProducer mp = s.createProducer(destination);\n" +
282
// "mp.send({2}(s,messageData));\n" +
283
// " '}' finally '{'\n" +
284
// "if (s != null) '{'\n"+
285
// " s.close();\n" +
286
// "'}'\n" +
287
// "if (conn != null) '{'\n" +
288
// "conn.close();\n" +
289
// "'}'\n" +
290
// "'}'\n",
291
// new Object[] {connectionName, destinationName, messageMethodName});
292
// }
293

294 }
295
296
Popular Tags