List of usage examples for java.lang Throwable toString
public String toString()
From source file:edu.utah.further.ds.impl.advice.QpMonitorAdvice.java
/** * @param throwable/*from ww w . j a va 2 s . c om*/ * @return */ private static String getExceptionAsString(final Throwable throwable) { String exceptionString = throwable.toString(); if (ReflectionUtil.instanceOf(throwable, ApplicationException.class)) { final ApplicationException error = (ApplicationException) throwable; if (error.getCode() != null) { if (error.getCode().isRecoverable()) { exceptionString = throwable.getClass() + ": " + throwable.getMessage(); } } } return exceptionString; }
From source file:com.cmsz.cloudplatform.utils.StringUtils.java
public static String getExceptionStackInfo(Throwable e) { StringBuffer sb = new StringBuffer(); sb.append(e.toString()).append("\n"); StackTraceElement[] elemnents = e.getStackTrace(); for (StackTraceElement element : elemnents) { sb.append(element.getClassName()).append("."); sb.append(element.getMethodName()).append("("); sb.append(element.getFileName()).append(":"); sb.append(element.getLineNumber()).append(")"); sb.append("\n"); }//from ww w. j ava 2 s . c o m return sb.toString(); }
From source file:eu.delving.sip.Harvester.java
static String exceptionToErrorString(Exception exception) { StringBuilder out = new StringBuilder(); out.append(exception.getMessage());/*from ww w .ja v a2s . c o m*/ Throwable cause = exception.getCause(); while (cause != null) { out.append('\n'); out.append(cause.toString()); cause = cause.getCause(); } return out.toString(); }
From source file:eu.faircode.netguard.ServiceJob.java
private static void submit(Rule rule, PersistableBundle bundle, Context context) { PackageManager pm = context.getPackageManager(); JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); // Get english application label String label = null;// w ww .j a v a 2 s. c om try { Configuration config = new Configuration(); config.setLocale(new Locale("en")); Resources res = pm.getResourcesForApplication(rule.info.packageName); res.updateConfiguration(config, res.getDisplayMetrics()); label = res.getString(rule.info.applicationInfo.labelRes); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); CharSequence cs = rule.info.applicationInfo.loadLabel(pm); if (cs != null) label = cs.toString(); } // Add application data bundle.putInt("uid", rule.info.applicationInfo.uid); bundle.putString("package", rule.info.packageName); bundle.putInt("version_code", rule.info.versionCode); bundle.putString("version_name", rule.info.versionName); bundle.putString("label", label); bundle.putInt("system", rule.system ? 1 : 0); try { bundle.putString("installer", pm.getInstallerPackageName(rule.info.packageName)); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); bundle.putString("installer", null); } // Cancel overlapping jobs for (JobInfo pending : scheduler.getAllPendingJobs()) { String type = pending.getExtras().getString("type"); if (type != null && type.equals(bundle.getString("type"))) { if (type.equals("rule")) { int uid = pending.getExtras().getInt("uid"); if (uid == bundle.getInt("uid")) { Log.i(TAG, "Canceling id=" + pending.getId()); scheduler.cancel(pending.getId()); } } else if (type.equals("host")) { int uid = pending.getExtras().getInt("uid"); int version = pending.getExtras().getInt("version"); int protocol = pending.getExtras().getInt("protocol"); String daddr = pending.getExtras().getString("daddr"); int dport = pending.getExtras().getInt("dport"); if (uid == bundle.getInt("uid") && version == bundle.getInt("version") && protocol == bundle.getInt("protocol") && daddr != null && daddr.equals(bundle.getString("daddr")) && dport == bundle.getInt("dport")) { Log.i(TAG, "Canceling id=" + pending.getId()); scheduler.cancel(pending.getId()); } } } } // Schedule job ComponentName serviceName = new ComponentName(context, ServiceJob.class); JobInfo job = new JobInfo.Builder(++id, serviceName).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setMinimumLatency(Util.isDebuggable(context) ? 10 * 1000 : 60 * 1000).setExtras(bundle) .setPersisted(true).build(); if (scheduler.schedule(job) == JobScheduler.RESULT_SUCCESS) Log.i(TAG, "Scheduled job=" + job.getId() + " success"); else Log.e(TAG, "Scheduled job=" + job.getId() + " failed"); }
From source file:com.norbl.util.StringUtil.java
public static String getExceptionMessage(Throwable x) { Throwable c = x.getCause();//w ww .java 2s. c o m if ((c != null) && (c.getMessage() != null) && (c.getMessage().length() > 0)) { return (c.getMessage()); } else return (x.toString()); }
From source file:com.uber.okbuck.core.util.ProjectUtil.java
public static Map<ComponentIdentifier, ResolvedArtifactResult> downloadSources(Project project, Set<ResolvedArtifactResult> artifacts) { // Download sources if enabled via intellij extension if (!ProjectUtil.getOkBuckExtension(project).getIntellijExtension().downloadSources()) { return new HashMap<>(); }//from w w w.j av a 2 s. co m DependencyHandler dependencies = project.getDependencies(); try { Set<ComponentIdentifier> identifiers = artifacts.stream() .filter(artifact -> canHaveSources(artifact.getFile())) .map(artifact -> artifact.getId().getComponentIdentifier()).collect(Collectors.toSet()); @SuppressWarnings("unchecked") Class<? extends Artifact>[] artifactTypesArray = (Class<? extends Artifact>[]) new Class<?>[] { SourcesArtifact.class }; Set<ComponentArtifactsResult> results = dependencies.createArtifactResolutionQuery() .forComponents(identifiers).withArtifacts(JvmLibrary.class, artifactTypesArray).execute() .getResolvedComponents(); // Only has one type. Class<? extends Artifact> type = artifactTypesArray[0]; return results.stream().map(artifactsResult -> artifactsResult.getArtifacts(type)).flatMap(Set::stream) .filter(artifactResult -> artifactResult instanceof ResolvedArtifactResult) .map(artifactResult -> (ResolvedArtifactResult) artifactResult) .filter(artifactResult -> FileUtil.isZipFile(artifactResult.getFile())) .collect(Collectors.toMap(resolvedArtifact -> resolvedArtifact.getId().getComponentIdentifier(), resolvedArtifact -> resolvedArtifact)); } catch (Throwable t) { System.out.println( "Unable to download sources for project " + project.toString() + " with error " + t.toString()); return new HashMap<>(); } }
From source file:com.ibm.mobilefirstplatform.serversdk.java.push.PushNotifications.java
/** * Send the given push notification, as configured, to devices using the Push Notification service. * /*from ww w . j a v a 2 s . c o m*/ * @param notification the push notification to be sent * @param listener an optional {@link PushNotificationsResponseListener} to listen to the result of this operation */ public static void send(JSONObject notification, PushNotificationsResponseListener listener) { if (pushMessageEndpointURL == null || pushMessageEndpointURL.length() == 0) { Throwable exception = new RuntimeException("PushNotifications has not been properly initialized."); logger.log(Level.SEVERE, exception.toString(), exception); if (listener != null) { listener.onFailure(null, null, exception); } return; } if (notification == null) { Throwable exception = new IllegalArgumentException("Cannot send a null push notification."); logger.log(Level.SEVERE, exception.toString(), exception); if (listener != null) { listener.onFailure(null, null, exception); } return; } CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost pushPost = createPushPostRequest(notification); executePushPostRequest(pushPost, httpClient, listener); }
From source file:com.hihframework.osplugins.json.JsonUtil.java
/** * ejson/*ww w.j a v a 2s .c om*/ * * @param e * @return */ public synchronized static String toString(Throwable e) { Throwable now = e; Throwable next = now.getCause(); while (next != null) { now = next; next = now.getCause(); } Map<String, String> map = new HashMap<String, String>(); map.put("error", now.toString()); map.put("message", now.getMessage() == null ? "" : now.getMessage()); return JSONObject.fromObject(map).toString(); }
From source file:com.ts.db.connector.ConnectorDriverManager.java
static Driver getDriver(String url, String driverClassName, String classpath) throws SQLException { assert !StringUtils.isBlank(url); final boolean hasClasspath = !StringUtils.isBlank(classpath); if (!hasClasspath) { for (Driver driver : new ArrayList<Driver>(drivers)) { if (driver.acceptsURL(url)) { return driver; }/* ww w . ja v a2 s . co m*/ } } List<File> jars = new ArrayList<File>(); ClassLoader cl; if (hasClasspath) { List<URL> urls = new ArrayList<URL>(); for (String path : classpath.split(pathSeparator)) { final File file = new File(path); if (isJarFile(file)) { jars.add(file); } try { urls.add(file.toURI().toURL()); } catch (MalformedURLException ex) { log.warn(ex.toString()); } } cl = new URLClassLoader(urls.toArray(new URL[urls.size()])); } else { jars.addAll(getJarFiles(".")); jars.addAll(driverFiles); List<URL> urls = new ArrayList<URL>(); for (File file : jars) { try { urls.add(file.toURI().toURL()); } catch (MalformedURLException ex) { log.warn(ex.toString()); } } cl = new URLClassLoader(urls.toArray(new URL[urls.size()]), ClassLoader.getSystemClassLoader()); } driverFiles.addAll(jars); final boolean hasDriverClassName = !StringUtils.isBlank(driverClassName); if (hasDriverClassName) { try { Driver driver = DynamicLoader.newInstance(driverClassName, cl); assert driver != null; return driver; } catch (DynamicLoadingException ex) { Throwable cause = (ex.getCause() != ex) ? ex.getCause() : ex; SQLException exception = new SQLException(cause.toString()); exception.initCause(cause); throw exception; } } final String jdbcDrivers = System.getProperty("jdbc.drivers"); if (!StringUtils.isBlank(jdbcDrivers)) { for (String jdbcDriver : jdbcDrivers.split(":")) { try { Driver driver = DynamicLoader.newInstance(jdbcDriver, cl); if (driver != null) { if (!hasClasspath) { drivers.add(driver); } return driver; } } catch (DynamicLoadingException ex) { log.warn(ex.toString()); } } } for (File jar : jars) { try { Driver driver = getDriver(jar, url, cl); if (driver != null) { if (!hasClasspath) { drivers.add(driver); } return driver; } } catch (IOException ex) { log.warn(ex.toString()); } } for (String path : System.getProperty("java.class.path", "").split(pathSeparator)) { if (isJarFile(path)) { Driver driver; try { driver = getDriver(new File(path), url, cl); if (driver != null) { drivers.add(driver); return driver; } } catch (IOException ex) { log.warn(ex.toString()); } } } throw new SQLException("driver not found"); }
From source file:DateUtil.java
/** * Parse RSS date format to Date object. * Example of RSS date:/*from w w w. j ava 2s . co m*/ * Sat, 23 Sep 2006 22:25:11 +0000 */ public static Date parseDate(String dateString) { Date pubDate = null; try { // Split date string to values // 0 = week day // 1 = day of month // 2 = month // 3 = year (could be with either 4 or 2 digits) // 4 = time // 5 = GMT int weekDayIndex = 0; int dayOfMonthIndex = 2; int monthIndex = 1; int yearIndex = 5; int timeIndex = 3; int gmtIndex = 4; String[] values = dateString.split(" "); int columnCount = values.length; // Wed Aug 29 20:14:27 +0000 2007 if (columnCount == 5) { // Expected format: // 09 Nov 2006 23:18:49 EST dayOfMonthIndex = 0; monthIndex = 1; yearIndex = 2; timeIndex = 3; gmtIndex = 4; } else if (columnCount == 7) { // Expected format: // Thu, 19 Jul 2007 00:00:00 N yearIndex = 4; timeIndex = 5; gmtIndex = 6; } else if (columnCount < 5 || columnCount > 6) { throw new Exception("Invalid date format: " + dateString); } // Day of month int dayOfMonth = Integer.parseInt(values[dayOfMonthIndex]); // Month String[] months = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; String monthString = values[monthIndex]; int month = 0; for (int monthEnumIndex = 0; monthEnumIndex < 12; monthEnumIndex++) { if (monthString.equals(months[monthEnumIndex])) { month = monthEnumIndex; } } // Year int year = Integer.parseInt(values[yearIndex]); if (year < 100) { year += 2000; } // Time String[] timeValues = values[timeIndex].split(":"); int hours = Integer.parseInt(timeValues[0]); int minutes = Integer.parseInt(timeValues[1]); int seconds = Integer.parseInt(timeValues[2]); pubDate = getCal(dayOfMonth, month, year, hours, minutes, seconds, values[gmtIndex]); } catch (Exception ex) { // TODO: Add exception handling code System.err.println("parseRssDate error while converting date string to object: " + dateString + "," + ex.toString()); } catch (Throwable t) { // TODO: Add exception handling code System.err.println("parseRssDate error while converting date string to object: " + dateString + "," + t.toString()); } return pubDate; }