⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 allocateresourceservlet.java

📁 Sun公司Dream项目
💻 JAVA
字号:
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * http://www.opensource.org/licenses/cddl1.php.  If 
 * applicable, add the following below this CDDL HEADER, 
 * with the fields enclosed by brackets "[]" replaced 
 * with your own identifying information: 
 * Portions Copyright [yyyy]
 * [name of copyright owner]
 */ 

/*
 * $(@)AllocateResourceServlet.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
 * 
 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
 */
package com.sun.sjc.idtv.vod.server.topologymanager;



import javax.xml.soap.*;

import javax.servlet.*;

import javax.servlet.http.*;

import javax.rmi.PortableRemoteObject;

import javax.ejb.*; 



import javax.xml.transform.*;



import java.util.*;

import java.io.*;



import com.sun.sjc.idtv.vod.shared.data.*;



public class AllocateResourceServlet extends HttpServlet {



    static MessageFactory fac = null;

    static {

        try {

            fac = MessageFactory.newInstance();

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    };

    static TopologyManager tm;

    static TopologyManagerHome tmHome;





    MessageFactory msgFactory;



    public void init(ServletConfig servletConfig) throws ServletException {



        super.init(servletConfig);



        javax.naming.InitialContext initContext = null; 

        Hashtable env = new java.util.Hashtable(1); 

        try { 

            initContext = new javax.naming.InitialContext(); 



            String JNDIName = "java:comp/env/ejb/TopologyManager"; 

            Object objref = initContext.lookup(JNDIName); 

            tmHome = (TopologyManagerHome)PortableRemoteObject.narrow(objref, TopologyManagerHome.class);

            tm = tmHome.create();  

	} catch (Exception e) {

	    throw new ServletException("Unable to connect to TopologyManager " + e.getMessage());

	}



        try {

            // Initialize it to the default.

            msgFactory = MessageFactory.newInstance();

        } catch (SOAPException ex) {

            throw new ServletException("Unable to create message factory " + ex.getMessage());

        }

    }

    

    public void doPost( HttpServletRequest req, 

			HttpServletResponse resp)

	throws ServletException, IOException {

	try {

	    // Get all the headers from the HTTP request.

	    MimeHeaders headers = getHeaders(req);



	    // Get the body of the HTTP request.

	    InputStream is = req.getInputStream();



	    // Now internalize the contents of a HTTP request and

	    // create a SOAPMessage

	    SOAPMessage msg = msgFactory.createMessage(headers, is);



	    SOAPMessage reply = null;



	    reply = onMessage(msg, req.getRemoteAddr());



	    if (reply != null) {

                

		// Need to call saveChanges because we're going to use the

		// MimeHeaders to set HTTP response information. These

		// MimeHeaders are generated as part of the save.



		if (reply.saveRequired()) {

		    reply.saveChanges(); 

		}



		resp.setStatus(HttpServletResponse.SC_OK);



		putHeaders(reply.getMimeHeaders(), resp);

                    

		// Write out the message on the response stream.

		OutputStream os = resp.getOutputStream();

		reply.writeTo(os);

    

		os.flush();

                    

	    } else 

		resp.setStatus(HttpServletResponse.SC_NO_CONTENT);



	} catch (Exception ex) {

	    throw new ServletException("JAXM POST failed "+ex.getMessage());

	}

    }



    static MimeHeaders getHeaders(HttpServletRequest req) {



	Enumeration enum1 = req.getHeaderNames();

	MimeHeaders headers = new MimeHeaders();



	while (enum1.hasMoreElements()) {

	    String headerName = (String)enum1.nextElement();

	    String headerValue = req.getHeader(headerName);



	    StringTokenizer values = new StringTokenizer(headerValue, ",");

	    while (values.hasMoreTokens())

		headers.addHeader(headerName, values.nextToken().trim());

	}



	return headers;

    }



    static void putHeaders(MimeHeaders headers, 

			   HttpServletResponse res) {



	Iterator it = headers.getAllHeaders();

	while (it.hasNext()) {

	    MimeHeader header = (MimeHeader)it.next();



	    String[] values = headers.getHeader(header.getName());

	    if (values.length == 1)

		res.setHeader(header.getName(), header.getValue());

	    else {

		StringBuffer concat = new StringBuffer();

		int i = 0;

		while (i < values.length) {

		    if (i != 0)

			concat.append(',');

		    concat.append(values[i++]);

		}



		res.setHeader(header.getName(), concat.toString());

	    }

	}

    }



    // This is the application code for handling the message.



    public SOAPMessage onMessage(SOAPMessage message, String requestIp) {

	SOAPMessage reply = null;

        try {

            //retrieve orderID from message received

	    SOAPBody receivedSB = message.getSOAPPart().getEnvelope().getBody();

        

	    Iterator receivedIt = receivedSB.getChildElements();

        SOAPBodyElement receivedSBE = (SOAPBodyElement)receivedIt.next();

        System.out.println("onMessage(): SOAPBodyElement.getElementName() = "+receivedSBE.getElementName().getLocalName());

        

        String requestType = receivedSBE.getElementName().getLocalName();

	    

        Iterator receivedIt2 = receivedSBE.getChildElements();

	    SOAPElement receivedSE = (SOAPElement)receivedIt2.next();



	    //get the cpeID

	    String cpeID = receivedSE.getValue();



    	//create reply message

	    //MessageFactory factory = MessageFactory.newInstance();

	    reply = fac.createMessage();

    	SOAPPart sp = reply.getSOAPPart();

	    SOAPEnvelope env = sp.getEnvelope();

        env.getHeader().detachNode();

    	SOAPBody sb = env.getBody();



        if(requestType.equals("allocate-qam-port")) {

            System.out.println("onMessage(): allocating QAM port");



    	    // get resource from topologymanager EJB

	        QamResource resource = tm.allocate(cpeID);

	        if (resource == null) {

    	    	resource = new QamResource(cpeID, "NO_RESOURCE_AVAILABLE", "", "", "");

	        }



    	    Name newBodyName = env.createName("allocate-qam-port-reply", 

	    				      "", 

	    				      "http://vod-vas");

    	    SOAPBodyElement replySBE = sb.addBodyElement(newBodyName);



    	    //dqa-ip

	        Name dqaIPName = env.createName("dqa-ip");

    	    SOAPElement dqaIP = replySBE.addChildElement(dqaIPName);

	        dqaIP.addTextNode(resource.getIP());



            //dqa-port

	        Name dqaPortName = env.createName("dqa-port");

    	    SOAPElement dqaPort = replySBE.addChildElement(dqaPortName);

	        dqaPort.addTextNode(resource.getPort());



            //ts-id

    	    Name tsIDName = env.createName("ts-id");

	        SOAPElement tsID = replySBE.addChildElement(tsIDName);

	        tsID.addTextNode(resource.getTSID());



            //rf-channel

    	    Name rfChannelName = env.createName("rf-channel");

	        SOAPElement rfChannel = replySBE.addChildElement(rfChannelName);

	        rfChannel.addTextNode(resource.getRFChannel());

        } 

        else if (requestType.equals("allocate-ip-port")) {

            System.out.println("onMessage(): allocating IP port");



    	    // get resource from topologymanager EJB

            System.out.println("onMessage(): requestIp = "+requestIp);

	        IpResource resource = tm.allocateIpResource(cpeID, requestIp);

	        if (resource == null) {

    	    	resource = new IpResource(cpeID, "");

	        }



    	    Name newBodyName = env.createName("allocate-ip-port-reply", 

	    				      "", 

	    				      "http://vod-vas");

    	    SOAPBodyElement replySBE = sb.addBodyElement(newBodyName);



    	    //serverip

	        Name serverIPName = env.createName("serverip");

    	    SOAPElement serverIP = replySBE.addChildElement(serverIPName);

	        serverIP.addTextNode(resource.getServerIp());

        }

        reply.saveChanges();

    	} catch (Exception ex) {

	        ex.printStackTrace();

	    }

	return reply;

    }

}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -