KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > console > JBIAuditPortlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.console;
18
19 import org.apache.servicemix.JbiConstants;
20 import org.apache.servicemix.jbi.audit.AuditorMBean;
21 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
22 import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
23
24 import javax.jbi.messaging.MessageExchange;
25 import javax.jbi.messaging.NormalizedMessage;
26 import javax.portlet.ActionRequest;
27 import javax.portlet.ActionResponse;
28 import javax.portlet.RenderRequest;
29
30 import java.net.URI JavaDoc;
31 import java.text.DateFormat JavaDoc;
32 import java.util.Calendar JavaDoc;
33 import java.util.Date JavaDoc;
34
35 public class JBIAuditPortlet extends ServiceMixPortlet {
36     
37     protected int page = 0;
38
39     public static class ExchangeInfo {
40         private String JavaDoc id;
41         private String JavaDoc date;
42         private String JavaDoc mep;
43         private String JavaDoc status;
44         
45         /**
46          * @return Returns the dateStamp.
47          */

48         public String JavaDoc getDate() {
49             return date;
50         }
51         /**
52          * @param dateStamp The dateStamp to set.
53          */

54         public void setDate(String JavaDoc dateStamp) {
55             this.date = dateStamp;
56         }
57         /**
58          * @return Returns the status.
59          */

60         public String JavaDoc getStatus() {
61             return status;
62         }
63         /**
64          * @param status The status to set.
65          */

66         public void setStatus(String JavaDoc status) {
67             this.status = status;
68         }
69         /**
70          * @return Returns the id.
71          */

72         public String JavaDoc getId() {
73             return id;
74         }
75         /**
76          * @param id The id to set.
77          */

78         public void setId(String JavaDoc id) {
79             this.id = id;
80         }
81         /**
82          * @return Returns the mep.
83          */

84         public String JavaDoc getMep() {
85             return mep;
86         }
87         /**
88          * @param mep The mep to set.
89          */

90         public void setMep(String JavaDoc mep) {
91             this.mep = mep;
92         }
93     }
94     
95     protected void doProcessAction(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception JavaDoc {
96         System.err.println(actionRequest.getParameterMap());
97         if (actionRequest.getParameter("view") != null) {
98             page = Integer.parseInt(actionRequest.getParameter("view"));
99         }
100     }
101
102     protected void fillViewRequest(RenderRequest request) throws Exception JavaDoc {
103         AuditorMBean auditor = getJdbcAuditor();
104         int count = auditor.getExchangeCount();
105         request.setAttribute("count", new Integer JavaDoc(count));
106         request.setAttribute("page", new Integer JavaDoc(page));
107         MessageExchange[] exchanges = auditor.getExchanges(page * 10, Math.min((page + 1) * 10, count));
108         request.setAttribute("exchanges", prepare(exchanges));
109         super.fillViewRequest(request);
110     }
111
112     private ExchangeInfo[] prepare(MessageExchange[] exchanges) throws Exception JavaDoc {
113         ExchangeInfo[] infos = new ExchangeInfo[exchanges.length];
114         for (int i = 0; i < infos.length; i++) {
115             infos[i] = new ExchangeInfo();
116             infos[i].id = exchanges[i].getExchangeId();
117             infos[i].mep = getMep(exchanges[i]);
118             infos[i].status = exchanges[i].getStatus().toString();
119             Object JavaDoc c = exchanges[i].getProperty(JbiConstants.DATESTAMP_PROPERTY_NAME);
120             if (c instanceof Calendar JavaDoc) {
121                 infos[i].date = DateFormat.getDateTimeInstance().format(((Calendar JavaDoc) c).getTime());
122             } else if (c instanceof Date JavaDoc) {
123                 infos[i].date = DateFormat.getDateTimeInstance().format((Date JavaDoc) c);
124             }
125         }
126         return infos;
127     }
128     
129     private String JavaDoc getMep(MessageExchange exchange) {
130         URI JavaDoc uri = exchange.getPattern();
131         if (MessageExchangeSupport.IN_ONLY.equals(uri)) {
132             return "In Only";
133         } else if (MessageExchangeSupport.IN_OPTIONAL_OUT.equals(uri)) {
134             return "In Opt Out";
135         } else if (MessageExchangeSupport.IN_OUT.equals(uri)) {
136             return "In Out";
137         } else if (MessageExchangeSupport.ROBUST_IN_ONLY.equals(uri)) {
138             return "Robust In Only";
139         } else {
140             return uri.toString();
141         }
142     }
143
144     private String JavaDoc prepareContent(NormalizedMessage msg) throws Exception JavaDoc {
145         if (msg != null) {
146             SourceTransformer st = new SourceTransformer();
147             String JavaDoc s = st.contentToString(msg);
148             if (s != null && s.length() > 30) {
149                 return s.substring(0, 30) + "...";
150             } else {
151                 return s;
152             }
153         } else {
154             return null;
155         }
156     }
157
158 }
159
Popular Tags