play2-source-watchers/play2-source-watcher-jnotify/src/main/java/com/google/code/play2/watcher/jnotify/JNotifyFileWatchService.java
@Override
    public void initialize( FileWatchLogger log ) throws FileWatchException
    {
        super.initialize( log );

        File nativeLibsDirectory = new File( "." ); // maybe change to temporary directory?
        String nativeLibsDirectoryProp = System.getProperty( "play2.nativeLibsDirectory" );
        if ( nativeLibsDirectoryProp != null && !"".equals( nativeLibsDirectoryProp ) )
        {
            nativeLibsDirectory = new File( nativeLibsDirectoryProp );
        }
        else
        {
            String targetDirectoryProp = System.getProperty( "project.build.directory" );
            if ( targetDirectoryProp != null && !"".equals( targetDirectoryProp ) )
            {
                File targetDirectory = new File( targetDirectoryProp );
                nativeLibsDirectory = new File( targetDirectory, "native_libraries" );
            }
        }
        String nativeLibsPath = nativeLibsDirectory.getAbsolutePath();

        if ( !nativeLibsDirectory.exists() && !nativeLibsDirectory.mkdirs() )
        {
            throw new FileWatchException( String.format( "Cannot create \"%s\" directory", nativeLibsPath ) );
        }

        String libraryOS = null;
        String libraryName = "jnotify";
        String osName = System.getProperty( "os.name" );
        if ( osName != null )
        {
            osName = osName.toLowerCase( Locale.ENGLISH );
            String architecture = System.getProperty( "sun.arch.data.model" );
            if ( osName.startsWith( "windows" ) )
            {
                libraryOS = "windows" + architecture;
                if ( "amd64".equals( System.getProperty( "os.arch" ) ) )
                {
                    libraryName = "jnotify_64bit";
                }
            }
            else if ( osName.equals( "linux" ) )
            {
                libraryOS = "linux" + architecture;
            }
            else if ( osName.startsWith( "mac os x" ) )
            {
                libraryOS = "osx";
            }
        }

        if ( libraryOS == null )
        {
            throw new FileWatchException(
                                          String.format( "JNotifyFileWatchService initialization failed - unsupported OS \"%s\"",
                                                         osName ) );
        }

        String libraryResourceName = System.mapLibraryName( libraryName );
        libraryResourceName = libraryResourceName.replace( ".dylib", ".jnilib" ); // fix for JDK-7134701 bug
        File outputFile = new File( nativeLibsDirectory, libraryResourceName );
        if ( !outputFile.exists() )
        {
            try
            {
                copyResourceToFile( "META-INF/native/" + libraryOS, libraryResourceName, nativeLibsDirectory );
            }
            catch ( IOException e )
            {
                throw new FileWatchException( "JNotifyFileWatchService initialization failed", e );
            }
        }

        // hack to update java.library.path
        try
        {
            String javaLibraryPath = System.getProperty( "java.library.path" );
            javaLibraryPath =
                javaLibraryPath != null ? javaLibraryPath + File.pathSeparator + nativeLibsPath : nativeLibsPath;
            System.setProperty( "java.library.path", javaLibraryPath );

            Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
            fieldSysPath.setAccessible( true );
            fieldSysPath.set( null, null );
        }
        catch ( Exception e )
        {
            throw new FileWatchException( "JNotifyFileWatchService initialization failed", e );
        }

        // initialize JNotify
        try
        {
            JNotify.removeWatch( 0 );
        }
        catch ( JNotifyException e )
        {
            throw new FileWatchException( "JNotifyFileWatchService initialization failed", e );
        }
    }
@Override
    public void initialize( FileWatchLogger log ) throws FileWatchException
    {
        super.initialize( log );

        File nativeLibsDirectory = new File( "." ); // maybe change to temporary directory?
        String nativeLibsDirectoryProp = System.getProperty( "play2.nativeLibsDirectory" );
        if ( nativeLibsDirectoryProp != null && !"".equals( nativeLibsDirectoryProp ) )
        {
            nativeLibsDirectory = new File( nativeLibsDirectoryProp );
        }
        else
        {
            String targetDirectoryProp = System.getProperty( "project.build.directory" );
            if ( targetDirectoryProp != null && !"".equals( targetDirectoryProp ) )
            {
                File targetDirectory = new File( targetDirectoryProp );
                nativeLibsDirectory = new File( targetDirectory, "native_libraries" );
            }
        }
        String nativeLibsPath = nativeLibsDirectory.getAbsolutePath();

        if ( !nativeLibsDirectory.exists() && !nativeLibsDirectory.mkdirs() )
        {
            throw new FileWatchException( String.format( "Cannot create \"%s\" directory", nativeLibsPath ) );
        }

        String libraryOS = null;
        String libraryName = "jnotify";
        String osName = System.getProperty( "os.name" );
        if ( osName != null )
        {
            osName = osName.toLowerCase( Locale.ENGLISH );
            String architecture = System.getProperty( "sun.arch.data.model" );
            if ( osName.startsWith( "windows" ) )
            {
                libraryOS = "windows" + architecture;
                if ( "amd64".equals( System.getProperty( "os.arch" ) ) )
                {
                    libraryName = "jnotify_64bit";
                }
            }
            else if ( osName.equals( "linux" ) )
            {
                libraryOS = "linux" + architecture;
            }
            else if ( osName.startsWith( "mac os x" ) )
            {
                libraryOS = "osx";
            }
        }

        if ( libraryOS == null )
        {
            throw new FileWatchException(
                                          String.format( "JNotifyFileWatchService initialization failed - unsupported OS \"%s\"",
                                                         osName ) );
        }

        String libraryResourceName = System.mapLibraryName( libraryName );
        libraryResourceName = libraryResourceName.replace( ".dylib", ".jnilib" ); // fix for JDK-7134701 bug
        File outputFile = new File( nativeLibsDirectory, libraryResourceName );
        if ( !outputFile.exists() )
        {
            try
            {
                copyResourceToFile( "META-INF/native/" + libraryOS, libraryResourceName, nativeLibsDirectory );
            }
            catch ( IOException e )
            {
                throw new FileWatchException( "JNotifyFileWatchService initialization failed", e );
            }
        }

        // hack to update java.library.path
        try
        {
            String javaLibraryPath = System.getProperty( "java.library.path" );
            javaLibraryPath =
                javaLibraryPath != null ? javaLibraryPath + File.pathSeparator + nativeLibsPath : nativeLibsPath;
            System.setProperty( "java.library.path", javaLibraryPath );

            Field fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
            fieldSysPath.setAccessible( true );
            fieldSysPath.set( null, null );
        }
        catch ( Exception e )
        {
            throw new FileWatchException( "JNotifyFileWatchService initialization failed", e );
        }

        // initialize JNotify
        try
        {
            JNotify.removeWatch( 0 );
        }
        catch ( JNotifyException e )
        {
            throw new FileWatchException( "JNotifyFileWatchService initialization failed", e );
        }
    }
@ Override public void initialize ( FileWatchLogger log ) throws FileWatchException { super . initialize ( log ) ; File nativeLibsDirectory = new File ( "." ) ; // maybe change to temporary directory? String nativeLibsDirectoryProp = System . getProperty ( "play2.nativeLibsDirectory" ) ; if ( nativeLibsDirectoryProp != null && ! "" . equals ( nativeLibsDirectoryProp ) ) { nativeLibsDirectory = new File ( nativeLibsDirectoryProp ) ; } else { String targetDirectoryProp = System . getProperty ( "project.build.directory" ) ; if ( targetDirectoryProp != null && ! "" . equals ( targetDirectoryProp ) ) { File targetDirectory = new File ( targetDirectoryProp ) ; nativeLibsDirectory = new File ( targetDirectory , "native_libraries" ) ; } } String nativeLibsPath = nativeLibsDirectory . getAbsolutePath ( ) ; if ( ! nativeLibsDirectory . exists ( ) && ! nativeLibsDirectory . mkdirs ( ) ) { throw new FileWatchException ( String . format ( "Cannot create \"%s\" directory" , nativeLibsPath ) ) ; } String libraryOS = null ; String libraryName = "jnotify" ; String osName = System . getProperty ( "os.name" ) ; if ( osName != null ) { osName = osName . toLowerCase ( Locale . ENGLISH ) ; String architecture = System . getProperty ( "sun.arch.data.model" ) ; if ( osName . startsWith ( "windows" ) ) { libraryOS = "windows" + architecture ; if ( "amd64" . equals ( System . getProperty ( "os.arch" ) ) ) { libraryName = "jnotify_64bit" ; } } else if ( osName . equals ( "linux" ) ) { libraryOS = "linux" + architecture ; } else if ( osName . startsWith ( "mac os x" ) ) { libraryOS = "osx" ; } } if ( libraryOS == null ) { throw new FileWatchException ( String . format ( "JNotifyFileWatchService initialization failed - unsupported OS \"%s\"" , osName ) ) ; } String libraryResourceName = System . mapLibraryName ( libraryName ) ; libraryResourceName = libraryResourceName . replace ( ".dylib" , ".jnilib" ) ; // fix for JDK-7134701 bug File outputFile = new File ( nativeLibsDirectory , libraryResourceName ) ; if ( ! outputFile . exists ( ) ) { try { copyResourceToFile ( "META-INF/native/" + libraryOS , libraryResourceName , nativeLibsDirectory ) ; } catch ( IOException e ) { throw new FileWatchException ( "JNotifyFileWatchService initialization failed" , e ) ; } } // hack to update java.library.path try { String javaLibraryPath = System . getProperty ( "java.library.path" ) ; javaLibraryPath = javaLibraryPath != null ? javaLibraryPath + File . pathSeparator + nativeLibsPath : nativeLibsPath ; System . setProperty ( "java.library.path" , javaLibraryPath ) ; Field fieldSysPath = ClassLoader . class . getDeclaredField ( "sys_paths" ) ; fieldSysPath . setAccessible ( true ) ; fieldSysPath . set ( null , null ) ; } catch ( Exception e ) { throw new FileWatchException ( "JNotifyFileWatchService initialization failed" , e ) ; } // initialize JNotify try { JNotify . removeWatch ( 0 ) ; } catch ( JNotifyException e ) { throw new FileWatchException ( "JNotifyFileWatchService initialization failed" , e ) ; } }
{@inheritDoc}
{