All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openejb.webadmin.clienttools.ViewEjbBean Maven / Gradle / Ivy

The newest version!
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.openejb.webadmin.clienttools;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ejb.Stateless;
import javax.ejb.RemoteHome;

import org.apache.openejb.webadmin.HttpRequest;
import org.apache.openejb.webadmin.HttpResponse;
import org.apache.openejb.webadmin.HttpSession;
import org.apache.openejb.webadmin.WebAdminBean;
import org.apache.openejb.webadmin.HttpHome;
import org.apache.openejb.assembler.classic.ContainerInfo;
import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
import org.apache.openejb.config.ConfigurationFactory;
import org.apache.openejb.core.CoreDeploymentInfo;
import org.apache.openejb.assembler.classic.ContainerInfo;
import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
import org.apache.openejb.assembler.classic.OpenEjbConfiguration;
import org.apache.openejb.assembler.classic.AppInfo;
import org.apache.openejb.assembler.classic.EjbJarInfo;
import org.apache.openejb.config.ConfigurationFactory;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.spi.ContainerSystem;
import org.apache.openejb.DeploymentInfo;
import org.apache.openejb.BeanType;

/**
 * @author David Blevins
 */
@Stateless(name = "ClientTools/ViewEjb")
@RemoteHome(HttpHome.class)
public class ViewEjbBean extends WebAdminBean implements Constants {

    public void preProcess(HttpRequest request, HttpResponse response)
        throws IOException {
    }

    public void postProcess(HttpRequest request, HttpResponse response)
        throws IOException {
    }

    public void writeHtmlTitle(PrintWriter out) throws IOException {
        out.write("Client Tools -- EJB Viewer");
    }

    public void writePageTitle(PrintWriter out) throws IOException {
        out.write("EJB Viewer");
    }

    public void writeBody(PrintWriter out) throws IOException {
        try {
            String ejb = request.getQueryParameter("ejb");
            if (ejb == null) {
                OpenEjbConfiguration configuration = SystemInstance.get().getComponent(OpenEjbConfiguration.class);
                for (AppInfo appInfo : configuration.containerSystem.applications) {
                    for (EjbJarInfo ejbJarInfo : appInfo.ejbJars) {
                        for (EnterpriseBeanInfo bean : ejbJarInfo.enterpriseBeans) {
                            out.print(""+ejbImg+"  "+bean.ejbDeploymentId+"
"); } } } } else { printEjb(ejb, out, request.getSession()); } } catch (Exception e) { out.println("FAIL: "); out.print(e.getMessage()); // throw e; //return; } } public void printEjb(String name, PrintWriter out, HttpSession session) throws Exception { SystemInstance system = SystemInstance.get(); ContainerSystem containerSystem = system.getComponent(ContainerSystem.class); String id = (name.startsWith("/")) ? name.substring(1, name.length()) : name; org.apache.openejb.DeploymentInfo ejb = containerSystem.getDeploymentInfo(id); if (ejb == null) { out.print("No such EJB: " + id); return; } String type = null; switch (ejb.getComponentType()) { case CMP_ENTITY : type = "EntityBean with Container-Managed Persistence"; break; case BMP_ENTITY : type = "EntityBean with Bean-Managed Persistence"; break; case STATEFUL : type = "Stateful SessionBean"; break; case STATELESS : type = "Stateless SessionBean"; break; case SINGLETON: type = "Singleton SessionBean"; break; case MANAGED: type = "Managed SessionBean"; break; default : type = "Unkown Bean Type"; break; } out.print("" + type + "
"); out.print(""); printRow("JNDI Name", name, out); boolean hasLocal = ejb.getLocalInterface() != null; boolean hasRemote = ejb.getRemoteInterface() != null; String remoteInterfaceClassRef; String homeInterfaceClassRef; if (hasRemote){ remoteInterfaceClassRef = getClassRef(ejb.getRemoteInterface()); homeInterfaceClassRef = getClassRef(ejb.getHomeInterface()); } else { remoteInterfaceClassRef = "none"; homeInterfaceClassRef = "none"; } printRow("Remote Interface",remoteInterfaceClassRef,out); printRow("Home Interface", homeInterfaceClassRef, out); if (hasLocal){ String clzz = getClassRef(ejb.getLocalInterface()); printRow("Local Interface",clzz,out); clzz = getClassRef(ejb.getLocalHomeInterface()); printRow("LocalHome Interface",clzz,out); } printRow("Bean Class", getClassRef(ejb.getBeanClass()), out); if (ejb.getComponentType() == BeanType.BMP_ENTITY || ejb.getComponentType() == BeanType.CMP_ENTITY) { printRow("Primary Key", getClassRef(ejb.getPrimaryKeyClass()), out); } out.print("
"); out.print("

Actions:
"); out.print(""); // Browse JNDI with this ejb //javax.servlet.http.HttpSession session = this.session; HashMap objects = (HashMap) session.getAttribute("objects"); if (objects == null) { objects = new HashMap(); session.setAttribute("objects", objects); } InitialContext ctx; Properties p = new Properties(); p.put( Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory"); p.put("openejb.loader", "embed"); ctx = new InitialContext(p); if (hasRemote){ Object obj = ctx.lookup(name); String objID = ejb.getHomeInterface().getName() + "@" + obj.hashCode(); objects.put(objID, obj); String invokerURL = "Invoke this EJB's home interface"; printRow(pepperImg, invokerURL, out); } if (hasLocal){ Object obj = ctx.lookup(name+"Local"); String objID = ejb.getLocalHomeInterface().getName() + "@" + obj.hashCode(); objects.put(objID, obj); String invokerURL = "Invoke this EJB's local home interface"; printRow(pepperImg, invokerURL, out); } Context enc = ((org.apache.openejb.core.CoreDeploymentInfo) ejb).getJndiEnc(); String ctxID = "enc" + enc.hashCode(); session.setAttribute(ctxID, enc); String jndiURL = "Browse this EJB's private JNDI namespace"; printRow(pepperImg, jndiURL, out); out.print("
"); } protected void printRow(String col1, String col2, PrintWriter out) throws IOException { out.print(""); out.print(col1); out.print(""); out.print(col2); out.print(""); } public String getClassRef(Class clazz) throws Exception { String name = clazz.getName(); return "" + name + ""; } public String getShortClassRef(Class clazz) throws Exception { if (clazz.isPrimitive()) { return "" + clazz.getName() + ""; } else if (clazz.isArray() && clazz.getComponentType().isPrimitive()) { return "" + clazz.getComponentType() + "[]"; } else if (clazz.isArray()) { String name = clazz.getComponentType().getName(); int dot = name.lastIndexOf(".") + 1; String shortName = name.substring(dot, name.length()); return "" + shortName + "[]"; } else { String name = clazz.getName(); int dot = name.lastIndexOf(".") + 1; String shortName = name.substring(dot, name.length()); return "" + shortName + ""; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy