2014-04-19 02:30:29 -04:00
package net.filebot ;
2009-01-04 13:28:28 -05:00
2012-07-14 03:58:53 -04:00
import static java.awt.GraphicsEnvironment.* ;
2016-03-09 11:02:36 -05:00
import static java.util.Arrays.* ;
2016-03-07 07:30:12 -05:00
import static java.util.stream.Collectors.* ;
2013-02-25 12:27:34 -05:00
import static javax.swing.JOptionPane.* ;
2016-03-02 10:55:06 -05:00
import static net.filebot.Logging.* ;
2014-04-19 02:30:29 -04:00
import static net.filebot.Settings.* ;
import static net.filebot.util.FileUtilities.* ;
2016-03-07 07:30:12 -05:00
import static net.filebot.util.XPathUtilities.* ;
2014-07-29 02:45:15 -04:00
import static net.filebot.util.ui.SwingUI.* ;
2009-03-13 20:30:24 -04:00
2012-07-14 03:58:53 -04:00
import java.awt.Desktop ;
import java.awt.Dialog.ModalityType ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import java.io.File ;
import java.io.IOException ;
2013-03-30 00:20:56 -04:00
import java.io.OutputStream ;
2012-07-14 03:58:53 -04:00
import java.net.URI ;
2016-03-07 07:30:12 -05:00
import java.net.URL ;
2013-03-30 00:20:56 -04:00
import java.nio.channels.Channels ;
import java.nio.channels.FileChannel ;
2014-11-06 23:54:21 -05:00
import java.nio.file.StandardOpenOption ;
2012-07-14 03:58:53 -04:00
import java.security.CodeSource ;
import java.security.Permission ;
import java.security.PermissionCollection ;
import java.security.Permissions ;
import java.security.Policy ;
import java.security.ProtectionDomain ;
2016-03-07 07:30:12 -05:00
import java.util.Map ;
2016-03-09 14:26:00 -05:00
import java.util.logging.Handler ;
2012-07-14 03:58:53 -04:00
import java.util.logging.Level ;
import javax.swing.JDialog ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
2013-02-25 12:27:34 -05:00
import javax.swing.JOptionPane ;
2012-07-14 03:58:53 -04:00
import javax.swing.JPanel ;
import javax.swing.SwingUtilities ;
import javax.swing.UIManager ;
2016-03-10 14:43:21 -05:00
import org.kohsuke.args4j.CmdLineException ;
import org.w3c.dom.Document ;
2014-04-19 02:30:29 -04:00
import net.filebot.cli.ArgumentBean ;
import net.filebot.cli.ArgumentProcessor ;
import net.filebot.format.ExpressionFormat ;
2014-08-05 07:55:28 -04:00
import net.filebot.mac.MacAppUtilities ;
2015-05-09 04:07:38 -04:00
import net.filebot.ui.FileBotMenuBar ;
2015-05-10 14:06:04 -04:00
import net.filebot.ui.GettingStartedStage ;
2014-04-19 02:30:29 -04:00
import net.filebot.ui.MainFrame ;
2016-03-09 11:18:20 -05:00
import net.filebot.ui.NotificationHandler ;
2014-04-19 02:30:29 -04:00
import net.filebot.ui.PanelBuilder ;
import net.filebot.ui.SinglePanelFrame ;
2014-08-27 08:08:00 -04:00
import net.filebot.ui.transfer.FileTransferable ;
2014-04-19 02:30:29 -04:00
import net.filebot.util.PreferencesMap.PreferencesEntry ;
import net.filebot.util.TeePrintStream ;
2014-06-23 11:09:43 -04:00
import net.miginfocom.swing.MigLayout ;
2009-03-14 06:20:59 -04:00
2007-12-23 14:28:04 -05:00
public class Main {
2013-10-17 03:11:03 -04:00
2014-04-13 15:35:23 -04:00
public static void main ( String [ ] argumentArray ) {
2012-07-14 03:58:53 -04:00
try {
// parse arguments
2014-04-13 15:35:23 -04:00
final ArgumentBean args = ArgumentBean . parse ( argumentArray ) ;
2013-10-17 03:11:03 -04:00
2013-12-23 22:24:02 -05:00
if ( args . printHelp ( ) | | args . printVersion ( ) | | ( ! ( args . runCLI ( ) | | args . clearCache ( ) | | args . clearUserData ( ) ) & & isHeadless ( ) ) ) {
2012-07-14 03:58:53 -04:00
System . out . format ( " %s / %s%n%n " , getApplicationIdentifier ( ) , getJavaRuntimeIdentifier ( ) ) ;
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
if ( args . printHelp ( ) | | ( ! args . printVersion ( ) & & isHeadless ( ) ) ) {
2014-04-15 08:14:43 -04:00
ArgumentBean . printHelp ( args , System . out ) ;
2012-07-14 03:58:53 -04:00
}
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
// just print help message or version string and then exit
System . exit ( 0 ) ;
}
2013-10-17 03:11:03 -04:00
2013-11-07 02:43:58 -05:00
if ( args . clearCache ( ) | | args . clearUserData ( ) ) {
if ( args . clearUserData ( ) ) {
2016-03-10 09:05:56 -05:00
System . out . println ( " Reset preferences " ) ;
2013-11-07 02:43:58 -05:00
Settings . forPackage ( Main . class ) . clear ( ) ;
}
2016-03-08 11:21:07 -05:00
// clear preferences and cache
2013-11-07 02:43:58 -05:00
if ( args . clearCache ( ) ) {
2016-03-10 09:05:56 -05:00
System . out . println ( " Clear cache and temporary files " ) ;
2016-03-05 16:06:26 -05:00
for ( File folder : getChildren ( getApplicationFolder ( ) . getCanonicalFile ( ) , FOLDERS ) ) {
2016-03-10 09:05:56 -05:00
System . out . println ( " * Delete " + folder ) ;
2016-03-08 11:21:07 -05:00
delete ( folder ) ;
2013-11-07 02:43:58 -05:00
}
CacheManager . getInstance ( ) . clearAll ( ) ;
}
// just clear cache and/or settings and then exit
System . exit ( 0 ) ;
}
2016-03-08 11:21:10 -05:00
// update system properties
initializeSystemProperties ( args ) ;
2016-03-10 09:05:56 -05:00
initializeLogging ( args ) ;
2013-10-17 03:11:03 -04:00
2012-12-11 06:19:14 -05:00
// make sure java.io.tmpdir exists
2016-03-05 16:06:26 -05:00
createFolders ( getApplicationTempFolder ( ) ) ;
2013-10-17 03:11:03 -04:00
2012-10-27 22:52:49 -04:00
// initialize this stuff before anything else
2016-03-06 13:11:30 -05:00
CacheManager . getInstance ( ) ;
2012-10-27 22:52:49 -04:00
initializeSecurityManager ( ) ;
2013-10-17 03:11:03 -04:00
2014-04-13 15:35:23 -04:00
// make sure we can access application arguments at any time
2016-03-05 16:06:26 -05:00
setApplicationArgumentArray ( argumentArray ) ;
2014-04-13 15:35:23 -04:00
2014-11-10 03:18:32 -05:00
// initialize history spooler
2016-03-05 16:06:26 -05:00
HistorySpooler . getInstance ( ) . setPersistentHistoryEnabled ( useRenameHistory ( ) ) ;
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
// CLI mode => run command-line interface and then exit
if ( args . runCLI ( ) ) {
2016-03-10 13:32:11 -05:00
int status = new ArgumentProcessor ( ) . run ( args ) ;
2012-07-14 03:58:53 -04:00
System . exit ( status ) ;
}
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
// GUI mode => start user interface
2016-03-09 11:02:36 -05:00
SwingUtilities . invokeAndWait ( ( ) - > startUserInterface ( args ) ) ;
2013-10-17 03:11:03 -04:00
2015-01-01 15:29:00 -05:00
// preload media.types (when loaded during DnD it will freeze the UI for a few hundred milliseconds)
2012-07-14 10:50:07 -04:00
MediaTypes . getDefault ( ) ;
2014-01-12 14:22:14 -05:00
2015-01-01 15:29:00 -05:00
// check for application updates
2012-07-14 03:58:53 -04:00
if ( ! " skip " . equals ( System . getProperty ( " application.update " ) ) ) {
try {
checkUpdate ( ) ;
} catch ( Exception e ) {
2016-03-09 15:36:28 -05:00
debug . log ( Level . WARNING , " Failed to check for updates " , e ) ;
2012-07-14 03:58:53 -04:00
}
}
2015-05-10 14:06:04 -04:00
// check if application help should be shown
if ( ! " skip " . equals ( System . getProperty ( " application.help " ) ) ) {
try {
checkGettingStarted ( ) ;
} catch ( Exception e ) {
2016-03-09 15:36:28 -05:00
debug . log ( Level . WARNING , " Failed to show Getting Started help " , e ) ;
2015-05-10 14:06:04 -04:00
}
}
2015-10-11 12:56:01 -04:00
} catch ( CmdLineException e ) {
// illegal arguments => print CLI error message
2012-07-14 03:58:53 -04:00
System . err . println ( e . getMessage ( ) ) ;
System . exit ( - 1 ) ;
2015-10-11 12:56:01 -04:00
} catch ( Throwable e ) {
// unexpected error => dump stack
2016-03-09 15:36:28 -05:00
debug . log ( Level . SEVERE , " Unexpected error during startup " , e ) ;
2015-10-11 12:56:01 -04:00
System . exit ( - 1 ) ;
2012-07-14 03:58:53 -04:00
}
}
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
private static void startUserInterface ( ArgumentBean args ) {
2016-03-10 09:05:56 -05:00
// use native LaF an all platforms
2016-03-09 11:02:36 -05:00
try {
UIManager . setLookAndFeel ( UIManager . getSystemLookAndFeelClassName ( ) ) ;
} catch ( Exception e ) {
2016-03-09 15:36:28 -05:00
debug . log ( Level . SEVERE , e . getMessage ( ) , e ) ;
2016-03-09 11:02:36 -05:00
}
// default frame
JFrame frame = new MainFrame ( MainFrame . createPanelBuilders ( ) ) ;
// single panel frame
if ( args . mode ! = null ) {
PanelBuilder [ ] selection = stream ( MainFrame . createPanelBuilders ( ) ) . filter ( p - > p . getName ( ) . matches ( args . mode ) ) . toArray ( PanelBuilder [ ] : : new ) ;
2015-12-14 04:47:29 -05:00
if ( selection . length = = 1 ) {
frame = new SinglePanelFrame ( selection [ 0 ] ) . publish ( new FileTransferable ( args . getFiles ( false ) ) ) ;
2015-12-14 04:50:42 -05:00
} else if ( selection . length > 1 ) {
frame = new MainFrame ( selection ) ;
2015-12-14 04:47:29 -05:00
} else {
2013-04-09 13:55:54 -04:00
throw new IllegalArgumentException ( " Illegal mode: " + args . mode ) ;
}
2012-07-14 03:58:53 -04:00
}
2013-10-17 03:11:03 -04:00
2013-04-09 14:20:52 -04:00
try {
// restore previous size and location
restoreWindowBounds ( frame , Settings . forPackage ( MainFrame . class ) ) ;
} catch ( Exception e ) {
2014-12-22 13:58:24 -05:00
// make sure the main window is not displayed out of screen bounds
frame . setLocation ( 120 , 80 ) ;
2013-04-09 14:20:52 -04:00
}
2013-10-17 03:11:03 -04:00
2013-02-25 12:27:34 -05:00
frame . addWindowListener ( new WindowAdapter ( ) {
2013-10-17 03:11:03 -04:00
2013-02-25 12:27:34 -05:00
@Override
public void windowClosing ( WindowEvent e ) {
e . getWindow ( ) . setVisible ( false ) ;
2014-10-09 13:30:43 -04:00
2016-03-09 11:02:36 -05:00
// make sure any long running operations are done now and not later on the shutdown hook thread
2013-02-25 12:27:34 -05:00
HistorySpooler . getInstance ( ) . commit ( ) ;
2013-10-17 03:11:03 -04:00
2015-10-02 06:24:58 -04:00
// show donation / review reminders to power users (more than 2000 renames)
2015-01-18 15:34:31 -05:00
int renameCount = HistorySpooler . getInstance ( ) . getPersistentHistoryTotalSize ( ) ;
2015-10-12 15:30:13 -04:00
if ( renameCount > 2000 & & Math . random ( ) < = 0 . 777 ) {
2015-03-09 04:55:10 -04:00
if ( isAppStore ( ) ) {
showAppStoreReviewReminder ( ) ;
} else {
2015-01-18 15:34:31 -05:00
showDonationReminder ( ) ;
}
2013-03-26 04:43:02 -04:00
}
2013-10-17 03:11:03 -04:00
2013-02-25 12:27:34 -05:00
System . exit ( 0 ) ;
}
} ) ;
2013-10-17 03:11:03 -04:00
2014-10-18 11:52:35 -04:00
// configure main window
2014-10-09 09:37:15 -04:00
if ( isMacApp ( ) ) {
2014-10-18 11:52:35 -04:00
// Mac OS X specific configuration
2014-10-09 09:37:15 -04:00
MacAppUtilities . initializeApplication ( ) ;
2014-08-05 07:55:28 -04:00
MacAppUtilities . setWindowCanFullScreen ( frame ) ;
2015-05-09 04:07:38 -04:00
MacAppUtilities . setDefaultMenuBar ( FileBotMenuBar . createHelp ( ) ) ;
2015-05-17 08:46:31 -04:00
} else if ( isUbuntuApp ( ) ) {
// Ubuntu specific configuration
2015-05-18 11:41:26 -04:00
String options = System . getenv ( " JAVA_TOOL_OPTIONS " ) ;
if ( options ! = null & & options . contains ( " jayatanaag.jar " ) ) {
2016-03-09 11:02:36 -05:00
// menu should be rendered via JAyatana on Ubuntu 15.04 and higher
frame . setJMenuBar ( FileBotMenuBar . createHelp ( ) ) ;
2015-05-18 11:41:26 -04:00
}
2015-05-17 08:46:31 -04:00
frame . setIconImages ( ResourceManager . getApplicationIcons ( ) ) ;
2014-10-18 11:52:35 -04:00
} else {
// Windows / Linux specific configuration
2015-05-10 16:33:21 -04:00
frame . setIconImages ( ResourceManager . getApplicationIcons ( ) ) ;
2014-07-30 21:20:27 -04:00
}
2012-07-14 03:58:53 -04:00
// start application
2015-05-09 04:07:38 -04:00
frame . setDefaultCloseOperation ( JFrame . DISPOSE_ON_CLOSE ) ;
2012-07-14 03:58:53 -04:00
frame . setVisible ( true ) ;
}
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
/ * *
* Show update notifications if updates are available
* /
private static void checkUpdate ( ) throws Exception {
2016-03-07 07:30:12 -05:00
Cache cache = Cache . getCache ( getApplicationName ( ) , CacheType . Persistent ) ;
2016-03-07 11:01:10 -05:00
Document dom = cache . xml ( " update.url " , s - > new URL ( getApplicationProperty ( s ) ) ) . expire ( Cache . ONE_WEEK ) . retry ( 0 ) . get ( ) ;
2013-10-17 03:11:03 -04:00
2016-03-07 07:30:12 -05:00
// parse update xml
2016-03-09 00:58:39 -05:00
final Map < String , String > update = streamElements ( dom . getFirstChild ( ) ) . collect ( toMap ( n - > {
return n . getNodeName ( ) ;
} , n - > {
return n . getTextContent ( ) . trim ( ) ;
} ) ) ;
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
// check if update is required
2016-03-07 07:30:12 -05:00
int latestRev = Integer . parseInt ( update . get ( " revision " ) ) ;
2013-10-17 03:11:03 -04:00
int currentRev = getApplicationRevisionNumber ( ) ;
if ( latestRev > currentRev & & currentRev > 0 ) {
2016-03-07 07:30:12 -05:00
SwingUtilities . invokeLater ( ( ) - > {
final JDialog dialog = new JDialog ( JFrame . getFrames ( ) [ 0 ] , update . get ( " title " ) , ModalityType . APPLICATION_MODAL ) ;
final JPanel pane = new JPanel ( new MigLayout ( " fill, nogrid, insets dialog " ) ) ;
dialog . setContentPane ( pane ) ;
2013-10-17 03:11:03 -04:00
2016-03-07 07:30:12 -05:00
pane . add ( new JLabel ( ResourceManager . getIcon ( " window.icon.medium " ) ) , " aligny top " ) ;
pane . add ( new JLabel ( update . get ( " message " ) ) , " gap 10, wrap paragraph:push " ) ;
2013-10-17 03:11:03 -04:00
2016-03-07 07:30:12 -05:00
pane . add ( newButton ( " Download " , ResourceManager . getIcon ( " dialog.continue " ) , evt - > {
openURI ( update . get ( " download " ) ) ;
dialog . setVisible ( false ) ;
} ) , " tag ok " ) ;
pane . add ( newButton ( " Details " , ResourceManager . getIcon ( " action.report " ) , evt - > {
openURI ( update . get ( " discussion " ) ) ;
} ) , " tag help2 " ) ;
pane . add ( newButton ( " Ignore " , ResourceManager . getIcon ( " dialog.cancel " ) , evt - > {
dialog . setVisible ( false ) ;
} ) , " tag cancel " ) ;
dialog . pack ( ) ;
dialog . setLocation ( getOffsetLocation ( dialog . getOwner ( ) ) ) ;
dialog . setVisible ( true ) ;
2012-07-14 03:58:53 -04:00
} ) ;
}
}
2013-10-17 03:11:03 -04:00
2015-05-10 14:06:04 -04:00
/ * *
* Show Getting Started to new users
* /
private static void checkGettingStarted ( ) throws Exception {
PreferencesEntry < String > started = Settings . forPackage ( Main . class ) . entry ( " getting.started " ) . defaultValue ( " 0 " ) ;
if ( " 0 " . equals ( started . getValue ( ) ) ) {
2015-09-12 11:49:47 -04:00
started . setValue ( " 1 " ) ;
started . flush ( ) ;
2016-03-07 07:30:12 -05:00
// open Getting Started
SwingUtilities . invokeLater ( ( ) - > GettingStartedStage . start ( ) ) ;
2015-05-10 14:06:04 -04:00
}
}
2013-03-26 04:43:02 -04:00
private static void showDonationReminder ( ) {
PreferencesEntry < String > donation = Settings . forPackage ( Main . class ) . entry ( " donation " ) . defaultValue ( " 0 " ) ;
int donationRev = Integer . parseInt ( donation . getValue ( ) ) ;
int currentRev = getApplicationRevisionNumber ( ) ;
if ( donationRev > = currentRev ) {
return ;
}
2013-10-17 03:11:03 -04:00
2016-03-07 07:30:12 -05:00
String message = String . format ( " <html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider a donation to me and my work. It will help to<br>make FileBot even better!<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html> " , HistorySpooler . getInstance ( ) . getPersistentHistoryTotalSize ( ) ) ;
String [ ] actions = { " Donate! :) " , donationRev > 0 ? " Not this time " : " Later " } ;
2013-03-26 04:43:02 -04:00
JOptionPane pane = new JOptionPane ( message , INFORMATION_MESSAGE , YES_NO_OPTION , ResourceManager . getIcon ( " message.donate " ) , actions , actions [ 0 ] ) ;
pane . createDialog ( null , " Please Donate " ) . setVisible ( true ) ;
2016-03-07 07:30:12 -05:00
2013-03-26 04:43:02 -04:00
if ( pane . getValue ( ) = = actions [ 0 ] ) {
2016-03-07 07:30:12 -05:00
openURI ( getDonateURL ( ) ) ;
donation . setValue ( String . valueOf ( currentRev ) ) ;
2013-03-26 04:43:02 -04:00
} else {
if ( donationRev > 0 & & donationRev < currentRev ) {
donation . setValue ( String . valueOf ( currentRev ) ) ;
}
}
}
2013-10-17 03:11:03 -04:00
2015-03-09 04:55:10 -04:00
private static void showAppStoreReviewReminder ( ) {
2015-01-18 15:34:31 -05:00
PreferencesEntry < String > donation = Settings . forPackage ( Main . class ) . entry ( " review " ) . defaultValue ( " 0 " ) ;
int donationRev = Integer . parseInt ( donation . getValue ( ) ) ;
if ( donationRev > 0 ) {
return ;
}
// make sure review reminder is shown at most once (per machine)
int currentRev = getApplicationRevisionNumber ( ) ;
donation . setValue ( String . valueOf ( currentRev ) ) ;
2016-03-07 07:30:12 -05:00
String message = String . format ( " <html><p style='font-size:16pt; font-weight:bold'>Thank you for using FileBot!</p><br><p>It has taken many nights to develop this application. If you enjoy using it,<br>please consider writing a nice little review on the %s.<p><p style='font-size:14pt; font-weight:bold'>You've renamed %,d files.</p><br><html> " , getAppStoreName ( ) , HistorySpooler . getInstance ( ) . getPersistentHistoryTotalSize ( ) ) ;
String [ ] actions = { " Review! I like FileBot. :) " , " Never! Don't bother me again. " } ;
2015-01-18 15:34:31 -05:00
JOptionPane pane = new JOptionPane ( message , INFORMATION_MESSAGE , YES_NO_OPTION , ResourceManager . getIcon ( " window.icon.large " ) , actions , actions [ 0 ] ) ;
pane . createDialog ( null , " Please rate FileBot " ) . setVisible ( true ) ;
if ( pane . getValue ( ) = = actions [ 0 ] ) {
2016-03-07 07:30:12 -05:00
openURI ( getAppStoreLink ( ) ) ;
}
}
private static void openURI ( String uri ) {
try {
Desktop . getDesktop ( ) . browse ( URI . create ( uri ) ) ;
} catch ( Exception e ) {
2016-03-09 15:36:28 -05:00
debug . log ( Level . SEVERE , " Failed to open URI: " + uri , e ) ;
2015-01-18 15:34:31 -05:00
}
}
2012-07-14 03:58:53 -04:00
private static void restoreWindowBounds ( final JFrame window , final Settings settings ) {
// store bounds on close
window . addWindowListener ( new WindowAdapter ( ) {
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
@Override
public void windowClosing ( WindowEvent e ) {
// don't save window bounds if window is maximized
if ( ! isMaximized ( window ) ) {
settings . put ( " window.x " , String . valueOf ( window . getX ( ) ) ) ;
settings . put ( " window.y " , String . valueOf ( window . getY ( ) ) ) ;
settings . put ( " window.width " , String . valueOf ( window . getWidth ( ) ) ) ;
settings . put ( " window.height " , String . valueOf ( window . getHeight ( ) ) ) ;
}
}
} ) ;
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
// restore bounds
int x = Integer . parseInt ( settings . get ( " window.x " ) ) ;
int y = Integer . parseInt ( settings . get ( " window.y " ) ) ;
int width = Integer . parseInt ( settings . get ( " window.width " ) ) ;
int height = Integer . parseInt ( settings . get ( " window.height " ) ) ;
window . setBounds ( x , y , width , height ) ;
}
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
/ * *
2013-02-07 03:34:08 -05:00
* Initialize default SecurityManager and grant all permissions via security policy . Initialization is required in order to run { @link ExpressionFormat } in a secure sandbox .
2012-07-14 03:58:53 -04:00
* /
private static void initializeSecurityManager ( ) {
try {
// initialize security policy used by the default security manager
// because default the security policy is very restrictive (e.g. no FilePermission)
Policy . setPolicy ( new Policy ( ) {
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
@Override
public boolean implies ( ProtectionDomain domain , Permission permission ) {
// all permissions
return true ;
}
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
@Override
public PermissionCollection getPermissions ( CodeSource codesource ) {
2013-10-17 03:11:03 -04:00
// VisualVM can't connect if this method does return
2012-07-14 03:58:53 -04:00
// a checked immutable PermissionCollection
return new Permissions ( ) ;
}
} ) ;
2013-10-17 03:11:03 -04:00
2012-07-14 03:58:53 -04:00
// set default security manager
System . setSecurityManager ( new SecurityManager ( ) ) ;
} catch ( Exception e ) {
// security manager was probably set via system property
2016-03-09 15:36:28 -05:00
debug . log ( Level . WARNING , e . getMessage ( ) , e ) ;
2012-07-14 03:58:53 -04:00
}
2009-05-02 19:34:04 -04:00
}
2013-10-17 03:11:03 -04:00
2016-03-08 11:21:10 -05:00
public static void initializeSystemProperties ( ArgumentBean args ) {
System . setProperty ( " http.agent " , String . format ( " %s %s " , getApplicationName ( ) , getApplicationVersion ( ) ) ) ;
System . setProperty ( " sun.net.client.defaultConnectTimeout " , " 10000 " ) ;
System . setProperty ( " sun.net.client.defaultReadTimeout " , " 60000 " ) ;
System . setProperty ( " swing.crossplatformlaf " , " javax.swing.plaf.nimbus.NimbusLookAndFeel " ) ;
System . setProperty ( " grape.root " , new File ( getApplicationFolder ( ) , " grape " ) . getAbsolutePath ( ) ) ;
System . setProperty ( " org.apache.commons.logging.Log " , " org.apache.commons.logging.impl.NoOpLog " ) ;
System . setProperty ( " unixfs " , Boolean . toString ( args . unixfs ) ) ;
System . setProperty ( " useExtendedFileAttributes " , Boolean . toString ( ! args . disableExtendedAttributes ) ) ;
System . setProperty ( " useCreationDate " , Boolean . toString ( ! args . disableExtendedAttributes ) ) ;
System . setProperty ( " application.rename.history " , Boolean . toString ( ! args . action . equalsIgnoreCase ( " test " ) ) ) ; // do not keep history of --action test rename operations
}
2016-03-10 09:05:56 -05:00
public static void initializeLogging ( ArgumentBean args ) throws IOException {
// tee stdout and stderr to log file if set
if ( args . logFile ! = null ) {
File logFile = new File ( args . logFile ) ;
if ( ! logFile . isAbsolute ( ) ) {
logFile = new File ( new File ( getApplicationFolder ( ) , " logs " ) , logFile . getPath ( ) ) . getAbsoluteFile ( ) ; // by default resolve relative paths against {applicationFolder}/logs/{logFile}
}
if ( ! logFile . exists ( ) & & ! logFile . getParentFile ( ) . mkdirs ( ) & & ! logFile . createNewFile ( ) ) {
throw new IOException ( " Failed to create log file: " + logFile ) ;
}
// open file channel and lock
FileChannel logChannel = FileChannel . open ( logFile . toPath ( ) , StandardOpenOption . CREATE , StandardOpenOption . WRITE , StandardOpenOption . APPEND ) ;
if ( args . logLock ) {
if ( args . getLogLevel ( ) = = Level . ALL ) {
debug . config ( " Locking " + logFile ) ;
}
logChannel . lock ( ) ;
}
OutputStream out = Channels . newOutputStream ( logChannel ) ;
System . setOut ( new TeePrintStream ( out , true , " UTF-8 " , System . out ) ) ;
System . setErr ( new TeePrintStream ( out , true , " UTF-8 " , System . err ) ) ;
}
if ( args . runCLI ( ) ) {
// CLI logging settings
log . setLevel ( args . getLogLevel ( ) ) ;
// set debug log level standard log level if lower
if ( debug . getLevel ( ) . intValue ( ) < log . getLevel ( ) . intValue ( ) ) {
debug . setLevel ( log . getLevel ( ) ) ;
}
} else {
// GUI logging settings
log . setLevel ( Level . INFO ) ;
log . addHandler ( new NotificationHandler ( getApplicationName ( ) ) ) ;
// log errors to file
try {
Handler error = createSimpleFileHandler ( new File ( getApplicationFolder ( ) , " error.log " ) , Level . WARNING ) ;
log . addHandler ( error ) ;
debug . addHandler ( error ) ;
} catch ( Exception e ) {
debug . log ( Level . WARNING , " Failed to initialize error log " , e ) ;
}
}
}
2007-12-23 14:28:04 -05:00
}