01: /**
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */package org.superbiz.servlet;
18:
19: import javax.annotation.Resource;
20: import javax.ejb.EJB;
21: import javax.servlet.ServletException;
22: import javax.servlet.ServletOutputStream;
23: import javax.servlet.http.HttpServlet;
24: import javax.servlet.http.HttpServletRequest;
25: import javax.servlet.http.HttpServletResponse;
26: import javax.sql.DataSource;
27: import javax.naming.InitialContext;
28: import javax.naming.NamingException;
29: import java.io.IOException;
30:
31: public class AnnotatedServlet extends HttpServlet {
32: @EJB
33: private AnnotatedEJBLocal localEJB;
34:
35: @EJB
36: private AnnotatedEJBRemote remoteEJB;
37:
38: @Resource
39: private DataSource ds;
40:
41: protected void doGet(HttpServletRequest request,
42: HttpServletResponse response) throws ServletException,
43: IOException {
44: response.setContentType("text/plain");
45: ServletOutputStream out = response.getOutputStream();
46:
47: out.println("Local EJB");
48: out.println("@EJB=" + localEJB);
49: if (localEJB != null) {
50: out.println("@EJB.getName()=" + localEJB.getName());
51: out.println("@EJB.getDs()=" + localEJB.getDs());
52: }
53: out.println("JNDI=" + lookupField("localEJB"));
54: out.println();
55:
56: out.println("Remote EJB");
57: out.println("@EJB=" + remoteEJB);
58: if (localEJB != null) {
59: out.println("@EJB.getName()=" + remoteEJB.getName());
60: }
61: out.println("JNDI=" + lookupField("remoteEJB"));
62: out.println();
63:
64: out.println("DataSource");
65: out.println("@Resource=" + ds);
66: out.println("JNDI=" + lookupField("ds"));
67: }
68:
69: private Object lookupField(String name) {
70: try {
71: return new InitialContext().lookup("java:comp/env/"
72: + getClass().getName() + "/" + name);
73: } catch (NamingException e) {
74: return null;
75: }
76: }
77: }
|