Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:cismet/cismet-commons into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
helllth committed Nov 12, 2012
2 parents c752e03 + 8d8e350 commit bdd0048
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 15 deletions.
16 changes: 8 additions & 8 deletions nbactions.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>CUSTOM-deploy</actionName>
<displayName>deploy</displayName>
<goals>
<goal>deploy</goal>
</goals>
</action>
</actions>
<action>
<actionName>CUSTOM-deploy</actionName>
<displayName>deploy</displayName>
<goals>
<goal>deploy</goal>
</goals>
</action>
</actions>
98 changes: 91 additions & 7 deletions src/main/java/de/cismet/netutil/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.util.prefs.Preferences;

import javax.swing.JOptionPane;

import de.cismet.tools.PasswordEncrypter;

/**
Expand Down Expand Up @@ -228,6 +230,21 @@ public void toPreferences() {
toPreferences(this);
}

/**
* DOCUMENT ME!
*/
public static void clear() {
final Preferences prefs = Preferences.userNodeForPackage(Proxy.class);

// won't use clear since we don't know if anybody else stored preferences for this package
prefs.remove(PROXY_HOST);
prefs.remove(PROXY_PORT);
prefs.remove(PROXY_USERNAME);
prefs.remove(PROXY_PASSWORD);
prefs.remove(PROXY_DOMAIN);
prefs.remove(PROXY_ENABLED);
}

/**
* Loads a <code>Proxy</code> instance from previously stored user preferences. If there are no host and port proxy
* information <code>null</code> will be returned. If the return value is non-null at least the host and the port is
Expand Down Expand Up @@ -266,13 +283,7 @@ public static void toPreferences(final Proxy proxy) {
final Preferences prefs = Preferences.userNodeForPackage(Proxy.class);

if ((proxy == null) || (proxy.getHost() == null) || proxy.getHost().isEmpty() || (proxy.getPort() < 1)) {
// won't use clear since we don't know if anybody else stored preferences for this package
prefs.remove(PROXY_HOST);
prefs.remove(PROXY_PORT);
prefs.remove(PROXY_USERNAME);
prefs.remove(PROXY_PASSWORD);
prefs.remove(PROXY_DOMAIN);
prefs.remove(PROXY_ENABLED);
clear();
} else {
prefs.put(PROXY_HOST, proxy.getHost());
prefs.putInt(PROXY_PORT, proxy.getPort());
Expand Down Expand Up @@ -320,4 +331,77 @@ public static Proxy fromSystem() {

return null;
}

/**
* DOCUMENT ME!
*
* @param args DOCUMENT ME!
*/
// NOTE: use cli library if there shall be more (complex) options
@SuppressWarnings("CallToThreadDumpStack")
public static void main(final String[] args) {
try {
if (args.length == 1) {
final String arg = args[0];
if ("-c".equals(arg) || "--clear".equals(arg)) { // NOI18N
clear();
showMessage("Proxy information cleared", false);
} else if ("-p".equals(arg) || "--print".equals(arg)) { // NOI18N
final Proxy proxy = fromPreferences();

if (proxy == null) {
showMessage("Proxy information not set", false); // NOI18N
} else {
showMessage(proxy.toString(), false);
}
} else {
printUsage();
System.exit(1);
}
} else {
printUsage();
System.exit(1);
}

System.exit(0);
} catch (final Exception e) {
showMessage("Something went wrong: " + e.getMessage(), true); // NOI18N
System.err.println("\n");
e.printStackTrace();
System.err.println();
System.exit(2);
}
}

/**
* DOCUMENT ME!
*
* @param message DOCUMENT ME!
* @param error DOCUMENT ME!
*/
private static void showMessage(final String message, final boolean error) {
if (System.console() == null) {
JOptionPane.showMessageDialog(
null,
message,
error ? "Error" : "Information", // NOI18N
error ? JOptionPane.ERROR_MESSAGE : JOptionPane.INFORMATION_MESSAGE);
} else {
if (error) {
System.err.println("\n" + message + "\n"); // NOI18N
} else {
System.out.println("\n" + message + "\n"); // NOI18N
}
}
}

/**
* DOCUMENT ME!
*/
private static void printUsage() {
showMessage("Supported parameters are:\n\n" // NOI18N
+ "-c --clear\t\tremoves all proxy settings\n" // NOI18N
+ "-p --print\t\tprints out the proxy settings", // NOI18N
true);
}
}

0 comments on commit bdd0048

Please sign in to comment.