使用Axis进行日志的处理(Handler链)

 

在服务器端写一个日志处理的Handler类:

 

package cn.com.test.ce.webservice;

 

import org.apache.axis.AxisFault;

import org.apache.axis.Handler;

import org.apache.axis.MessageContext;

import org.apache.axis.handlers.BasicHandler;

 

import java.io.FileOutputStream;

import java.io.PrintWriter;

import java.util.Date;

 

public class LogHandler extends BasicHandler {

    public void invoke(MessageContext msgContext) throws AxisFault

    {

        /** Log an access each time we get invoked.

         */

        try {

            Handler serviceHandler = msgContext.getService();

            String filename = (String)getOption("filename");

            if ((filename == null) || (filename.equals("")))

                throw new AxisFault("Server.NoLogFile", "没有配置日志",null, null);

            FileOutputStream fos = new FileOutputStream(filename, true);

            PrintWriter writer = new PrintWriter(fos);

            Integer numAccesses =(Integer)serviceHandler.getOption("accesses");

            if (numAccesses == null)

                numAccesses = new Integer(0);

            numAccesses = new Integer(numAccesses.intValue() + 1);

           

            Date date = new Date();

            String result = date + ": service " +msgContext.getTargetService() +

                  " 访问 " + numAccesses + " time(s).";

            serviceHandler.setOption("accesses", numAccesses);

           

            writer.println(result);

           

            writer.close();

        } catch (Exception e) {

            throw AxisFault.makeFault(e);

        }

    }

}

 

 

再来一个服务类:指定哪些方法需要加上日志:

 

 

package cn.com.test.ce.webservice;

 

public class Service

{

    public String testMethod()

    {

        return "测试方法";

    }

}

 

 

客户端deploy.wsdd文件:

 

            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

客户端测试方法:

 

 

/*

 * Copyright 2001-2004 The Apache Software Foundation.

 *

 * Licensed 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 cn.com.test.webservice;

 

import java.net.URL;

 

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import org.apache.axis.utils.Options;

 

import javax.xml.namespace.QName;

 

public class Client

{

    public static void main(String [] args)

    {

        try {

         

                 //创建服务对象

                       Service service = new Service();

                       //通过服务对象创建调用对象

                       Call call=(Call)service.createCall();

                               

                               

                                String url = "http://localhost:8080/MyAxisLogHandlerWebService/services/LogTestService";

                                //设置调用的目标终端地址

                                call.setTargetEndpointAddress(new URL(url));

                                //设置对象的操作名称

                                call.setOperationName("testMethod");

          //  call.setOperationName( new QName("LogTestService", "testMethod") );

 

            String res = (String) call.invoke( new Object[] {} );

 

            System.out.println( res );

        } catch (Exception e) {

            System.err.println(e.toString());

        }

    }

}

 

你可能感兴趣的:(webService)