private void initAppHome()

in archiva-jarinfo/archiva-jarinfo-cli/src/main/java/org/apache/archiva/jarinfo/scanner/LibLocator.java [149:222]


    private void initAppHome()
    {
        /* Try System Property */
        String sysPropHome = System.getProperty( APP_HOME_PROPERTY );
        if ( ( sysPropHome != null ) && ( sysPropHome.trim().length() > 0 ) )
        {
            debug( "Found System Property." );
            this.appHomeDir = new File( sysPropHome.trim() );
            return;
        }

        /* Attempt to figgure out the classpath entry that this object was loaded from. */
        String selfclass = "/" + this.getClass().getName();
        selfclass = selfclass.replace( '.', '/' ) + ".class";
        URL classURL = this.getClass().getResource( selfclass );
        if ( classURL != null )
        {
            debug( "Found Class URL: " + classURL.toExternalForm() );
            String protocol = classURL.getProtocol();

            if ( "jar".equals( protocol ) )
            {
                debug( "Found in jar source: " + classURL.toExternalForm() );
                String rawurl = classURL.toExternalForm();
                if ( !rawurl.startsWith( "jar:file:/" ) )
                {
                    throw new IllegalStateException( "Unknown jar file syntax: " + rawurl );
                }

                rawurl = rawurl.substring( "jar:file:".length() );
                while ( rawurl.startsWith( "//" ) )
                {
                    rawurl = rawurl.substring( 1 );
                }

                int idx = rawurl.indexOf( ".jar!/" );
                if ( idx > 0 )
                {
                    rawurl = rawurl.substring( 0, idx );
                    debug( "Raw url stripped of class: " + rawurl );
                }

                idx = rawurl.lastIndexOf( "/", idx );
                if ( idx > 0 )
                {
                    rawurl = rawurl.substring( 0, idx );
                    debug( "Raw url path stripped: " + rawurl );
                }

                debug( "Found in a jar file: " + rawurl );
                this.appHomeDir = new File( rawurl );
            }
            else if ( "file".equals( protocol ) )
            {
                debug( "Found as file source: " + classURL.toExternalForm() );
                String rawurl = classURL.toExternalForm().substring( 6 );
                debug( "rawurl: " + rawurl );
                debug( "Found in a classpath directory." );
                this.appHomeDir = new File( rawurl.substring( 0, rawurl.length() - selfclass.length() ) );
            }
            else
            {
                throw new IllegalStateException( "Only jar and file sources ares supported by Launcher: "
                    + classURL.toExternalForm() );
            }
        }

        if ( this.appHomeDir == null )
        {
            throw new IllegalStateException( "Unable to find app classes." );
        }

        System.setProperty( APP_HOME_PROPERTY, this.appHomeDir.getAbsolutePath() );
    }