1 19 20 26 27 package org.netbeans.modules.exceptions; 28 import java.io.BufferedOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import java.io.ObjectOutputStream ; 32 import java.io.OutputStream ; 33 import java.io.Reader ; 34 import java.net.ConnectException ; 35 import java.net.HttpURLConnection ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.text.MessageFormat ; 39 import java.util.LinkedList ; 40 import java.util.logging.LogRecord ; 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 52 public class Sender { 53 private static final String uri = ExceptionsRepos.uri_server + "LoadingServlet"; 55 private static Record rec = new Record (); 56 private static final int MESSAGE_LENGTH = 41; 57 58 public Sender() { 59 } 60 61 62 public static String getOS(){ 63 String unknown = "unknown"; 64 String str = System.getProperty("os.name", unknown)+", "+ System.getProperty("os.version", unknown)+", "+ System.getProperty("os.arch", unknown); return str; 68 } 69 70 public static String getVersion(){ 71 String str = MessageFormat.format( 72 NbBundle.getBundle("org.netbeans.core.startup.Bundle").getString("currentVersion"), new Object [] {System.getProperty("netbeans.buildnumber")}); return str; 75 } 76 77 public static String getVM(){ 78 return System.getProperty("java.vm.name", "unknown") + ", " + System.getProperty("java.vm.version", ""); } 80 81 public static String getUserName(){ 82 return new ExceptionsSettings().getUserName(); 83 } 84 85 public static String getPassword(){ 86 return new ExceptionsSettings().getPassword(); 87 } 88 89 public static synchronized HttpURLConnection getHttpConnection(String myUri){ 90 HttpURLConnection conn=null; 91 try { 92 conn = (HttpURLConnection ) new URL (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"); conn.setRequestProperty("Pragma", "no-cache"); conn.setRequestProperty("Cache-control", "no-cache"); conn.connect(); 102 } catch (IOException 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 userName, String passwd){ 110 try { 111 HttpURLConnection conn = getHttpConnection(uri); 112 OutputStream oStream = conn.getOutputStream(); 113 ObjectOutputStream stream = new ObjectOutputStream (new BufferedOutputStream (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 lrec = rec.getLogger().getFirst(); 121 LogRecord first = rec.getLogger().removeFirst(); stream.writeObject(rec); 125 rec.getLogger().addFirst(first); stream.flush(); 127 oStream.flush(); 128 oStream.close(); 129 Reader reader = new InputStreamReader (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 (buffer), 134 NotifyDescriptor.INFORMATION_MESSAGE)); 135 conn.disconnect(); 140 } catch (ConnectException ex) { 141 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(NbBundle.getMessage(Sender.class, "NOT_REPORTED"), 142 NotifyDescriptor.INFORMATION_MESSAGE)); 143 } catch (IOException 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 _component, String _subComponent, String _assignedTo, String _summary, String _comment){ 151 String [] userData = new String [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 <LogRecord > listLogRec){ 161 LogRecord trans = listLogRec.getFirst(); 162 Throwable 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 (); } 173 174 public static String getActivIssueMessage(){ 175 String 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 [] args) throws MalformedURLException , IOException { 185 204 } 205 } 206 207 208 | Popular Tags |