KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > Sender


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * Sender.java
22  *
23  * Created on November 3, 2006, 11:36 AM
24  *
25  */

26
27 package org.netbeans.modules.exceptions;
28 import java.io.BufferedOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.ObjectOutputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.net.ConnectException JavaDoc;
35 import java.net.HttpURLConnection JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.text.MessageFormat JavaDoc;
39 import java.util.LinkedList JavaDoc;
40 import java.util.logging.LogRecord JavaDoc;
41 import org.netbeans.modules.exceptions.data.Record;
42 import org.netbeans.modules.exceptions.settings.ExceptionsRepos;
43 import org.netbeans.modules.exceptions.settings.ExceptionsSettings;
44 import org.openide.DialogDisplayer;
45 import org.openide.NotifyDescriptor;
46 import org.openide.util.NbBundle;
47
48 /**
49  *
50  * @author jindra
51  */

52 public class Sender {
53     private static final String JavaDoc uri = ExceptionsRepos.uri_server + "LoadingServlet"; // NOI18N
54

55     private static Record JavaDoc rec = new Record JavaDoc();
56     private static final int MESSAGE_LENGTH = 41;
57     /** Creates a new instance of Sender */
58     public Sender() {
59     }
60     
61     
62     public static String JavaDoc getOS(){
63         String JavaDoc unknown = "unknown";
64         String JavaDoc str = System.getProperty("os.name", unknown)+", "+ // NOI18N
65
System.getProperty("os.version", unknown)+", "+ // NOI18N
66
System.getProperty("os.arch", unknown); // NOI18N
67
return str;
68     }
69     
70     public static String JavaDoc getVersion(){
71         String JavaDoc str = MessageFormat.format(
72                 NbBundle.getBundle("org.netbeans.core.startup.Bundle").getString("currentVersion"), // NOI18N
73
new Object JavaDoc[] {System.getProperty("netbeans.buildnumber")}); // NOI18N
74
return str;
75     }
76     
77     public static String JavaDoc getVM(){
78         return System.getProperty("java.vm.name", "unknown") + ", " + System.getProperty("java.vm.version", ""); // NOI18N
79
}
80     
81     public static String JavaDoc getUserName(){
82         return new ExceptionsSettings().getUserName();
83     }
84     
85     public static String JavaDoc getPassword(){
86         return new ExceptionsSettings().getPassword();
87     }
88     
89     public static synchronized HttpURLConnection JavaDoc getHttpConnection(String JavaDoc myUri){
90         HttpURLConnection JavaDoc conn=null;
91         try {
92             conn = (HttpURLConnection JavaDoc) new URL JavaDoc(myUri).openConnection();
93             conn.setReadTimeout(2000);
94             conn.setDoOutput(true);
95             conn.setDoInput(true);
96             conn.setUseCaches(false);
97             conn.setDefaultUseCaches(false);
98             conn.setRequestMethod("PUT"); // NOI18N
99
conn.setRequestProperty("Pragma", "no-cache"); // NOI18N
100
conn.setRequestProperty("Cache-control", "no-cache"); // NOI18N
101
conn.connect();
102         } catch (IOException JavaDoc ex) {
103             java.util.logging.Logger.getLogger(ExceptionsRepos.class.getName()).log(java.util.logging.Level.WARNING,
104                     NbBundle.getBundle(ExceptionsRepos.class).getString("NO_CONNECTION"));
105         }
106         return conn;
107     }
108     
109     public static void send(String JavaDoc userName, String JavaDoc passwd){
110         try {
111             HttpURLConnection JavaDoc conn = getHttpConnection(uri);
112             OutputStream JavaDoc oStream = conn.getOutputStream();
113             ObjectOutputStream JavaDoc stream = new ObjectOutputStream JavaDoc(new BufferedOutputStream JavaDoc(oStream));
114             
115             rec.setOs(getOS());
116             rec.setVersion(getVersion());
117             rec.setVm(getVM());
118             if (userName != null)rec.setUserName(userName);
119             else rec.setUserName(getUserName());
120             final LogRecord JavaDoc lrec = rec.getLogger().getFirst();
121             // if (!ExceptionsRepos.getInstance().contains(lrec)){
122
//stream.reset();
123
LogRecord JavaDoc first = rec.getLogger().removeFirst();//it was there just for displaying
124
stream.writeObject(rec);
125             rec.getLogger().addFirst(first);//giving back for future usement
126
stream.flush();
127             oStream.flush();
128             oStream.close();
129             Reader JavaDoc reader = new InputStreamReader JavaDoc(conn.getInputStream());
130             char[] buffer = new char[MESSAGE_LENGTH];
131             reader.read(buffer, 0, MESSAGE_LENGTH);
132             ExceptionsRepos.getInstance().addRecord(lrec);
133             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(new String JavaDoc(buffer),
134                     NotifyDescriptor.INFORMATION_MESSAGE));
135             // }else{
136
// DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getMessage(Sender.class, "ALREADY_REPORTED"),
137
// NotifyDescriptor.INFORMATION_MESSAGE));
138
// }
139
conn.disconnect();
140         } catch (ConnectException JavaDoc ex) {
141             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getMessage(Sender.class, "NOT_REPORTED"),
142                     NotifyDescriptor.INFORMATION_MESSAGE));
143         } catch (IOException JavaDoc ex) {
144             DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getMessage(Sender.class, "NOT_REPORTED"),
145                     NotifyDescriptor.INFORMATION_MESSAGE));
146         }
147     }
148     
149     
150     public static void setND(String JavaDoc _component, String JavaDoc _subComponent, String JavaDoc _assignedTo, String JavaDoc _summary, String JavaDoc _comment){
151         String JavaDoc [] userData = new String JavaDoc[Record.ITEMS_COUNT];
152         userData[0]= _component;
153         userData[1]=_subComponent;
154         userData[2]=_assignedTo;
155         userData[3]=_summary;
156         userData[4]=_comment;
157         rec.setUserData(userData);
158     }
159     
160     public static void setTransport(LinkedList JavaDoc<LogRecord JavaDoc> listLogRec){
161         LogRecord JavaDoc trans = listLogRec.getFirst();
162         Throwable JavaDoc thrown = trans.getThrown();
163         rec.setThrowable(thrown.getStackTrace());
164         rec.setClassName(thrown.getClass().getName());
165         rec.setMessage(thrown.getMessage());
166         rec.setSeverity(trans.getLevel());
167         rec.setLogger(listLogRec);
168     }
169     
170     public static void clean(){
171         rec = new Record JavaDoc();// the old class will be removed by garbage collector
172
}
173
174     public static String JavaDoc getActivIssueMessage(){
175         String JavaDoc result;
176         if (rec != null){
177             result = rec.getClassName();
178             if (rec.getMessage() != null) return result+rec.getMessage();
179             else return result;
180         }else return null;
181     }
182     
183     
184     public static void main(String JavaDoc[] args) throws MalformedURLException JavaDoc, IOException JavaDoc{
185 /* String Url="http://localhost:8084/WebApplication8/NewServlet";
186         URL url = new URL(Url);
187         HttpURLConnection connection =(HttpURLConnection) url.openConnection();
188         connection.setRequestMethod("GET");
189         connection.setDoOutput(true);
190         OutputStreamWriter ostream = new OutputStreamWriter(connection.getOutputStream());
191         BufferedWriter out = new BufferedWriter(ostream);
192         out.write("AHOJ LIDI, TOHLE POSILAM");
193         out.flush();
194         out.close();
195         connection.disconnect();
196  
197         Collector coll = Collector.getDefault();
198         LogRecord log = new LogRecord(Level.SEVERE, "null pointer exception");
199         log.setThrown(new NullPointerException("NPE"));
200         coll.write(log);
201         coll.write(new LogRecord(Level.INFO, "TOHLE JSEM TI POSLAL!@"));
202         Sender.send();
203  */

204     }
205 }
206
207
208
Popular Tags