1/*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.xml.internal.ws.dump;
27
28import com.sun.xml.internal.ws.api.message.Packet;
29import com.sun.xml.internal.ws.api.pipe.Fiber;
30import com.sun.xml.internal.ws.api.pipe.NextAction;
31import com.sun.xml.internal.ws.api.pipe.Tube;
32import com.sun.xml.internal.ws.api.pipe.TubeCloner;
33import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
34import com.sun.xml.internal.ws.commons.xmlutil.Converter;
35import com.sun.xml.internal.ws.dump.MessageDumper.ProcessingState;
36
37import java.util.concurrent.atomic.AtomicInteger;
38import java.util.logging.Level;
39import java.util.logging.Logger;
40
41/**
42 *
43 * @author Marek Potociar <marek.potociar at sun.com>
44 */
45public class LoggingDumpTube extends AbstractFilterTubeImpl {
46    public static enum Position {
47        Before(MessageDumper.ProcessingState.Received, MessageDumper.ProcessingState.Processed),
48        After(MessageDumper.ProcessingState.Processed, MessageDumper.ProcessingState.Received);
49
50        private final MessageDumper.ProcessingState requestState;
51        private final MessageDumper.ProcessingState responseState;
52
53        private Position(ProcessingState requestState, ProcessingState responseState) {
54            this.requestState = requestState;
55            this.responseState = responseState;
56        }
57    }
58
59    private static final AtomicInteger ID_GENERATOR = new AtomicInteger(0);
60    //
61    private MessageDumper messageDumper;
62    private final Level loggingLevel;
63    private final Position position;
64    private final int tubeId;
65
66    public LoggingDumpTube(Level loggingLevel, Position position, Tube tubelineHead) {
67        super(tubelineHead);
68
69        this.position = position;
70        this.loggingLevel = loggingLevel;
71
72        this.tubeId = ID_GENERATOR.incrementAndGet();
73    }
74
75    public void setLoggedTubeName(String loggedTubeName) {
76        assert messageDumper == null; // must not set a new message dumper once already set
77        this.messageDumper = new MessageDumper(loggedTubeName, Logger.getLogger(loggedTubeName), loggingLevel);
78    }
79
80    /**
81     * Copy constructor.
82     */
83    private LoggingDumpTube(LoggingDumpTube original, TubeCloner cloner) {
84        super(original, cloner);
85
86        this.messageDumper = original.messageDumper;
87        this.loggingLevel = original.loggingLevel;
88        this.position = original.position;
89
90        this.tubeId = ID_GENERATOR.incrementAndGet();
91    }
92
93    public LoggingDumpTube copy(TubeCloner cloner) {
94        return new LoggingDumpTube(this, cloner);
95    }
96
97
98    @Override
99    public NextAction processRequest(Packet request) {
100        if (messageDumper.isLoggable()) {
101            Packet dumpPacket = (request != null) ? request.copy(true) : null;
102            messageDumper.dump(MessageDumper.MessageType.Request, position.requestState, Converter.toString(dumpPacket), tubeId, Fiber.current().owner.id);
103        }
104
105        return super.processRequest(request);
106    }
107
108    @Override
109    public NextAction processResponse(Packet response) {
110        if (messageDumper.isLoggable()) {
111            Packet dumpPacket = (response != null) ? response.copy(true) : null;
112            messageDumper.dump(MessageDumper.MessageType.Response, position.responseState, Converter.toString(dumpPacket), tubeId, Fiber.current().owner.id);
113        }
114
115        return super.processResponse(response);
116    }
117
118    @Override
119    public NextAction processException(Throwable t) {
120        if (messageDumper.isLoggable()) {
121            messageDumper.dump(MessageDumper.MessageType.Exception, position.responseState, Converter.toString(t), tubeId, Fiber.current().owner.id);
122        }
123
124        return super.processException(t);
125    }
126
127    @Override
128    public void preDestroy() {
129        super.preDestroy();
130    }
131}
132