001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.fukurou.util;
017
018import org.opengion.fukurou.system.OgRuntimeException ;         // 6.4.2.0 (2016/01/29)
019import java.io.IOException;
020import java.io.File;
021
022import java.util.List;
023import java.util.ArrayList;
024import java.util.Enumeration;
025import java.util.jar.JarFile;
026import java.util.jar.JarEntry;
027import java.net.URL;
028
029import org.opengion.fukurou.system.Closer;                                                      // 6.4.2.0 (2016/01/29) package変更 fukurou.util → fukurou.system
030
031/**
032 * このクラスは、指定のディレクトリパスから .class ファイルを検索するクラスです。
033 * 検索パスは、実ファイルと、zipファイルの内部、jar ファイルの内部も含みます。
034 * 検索結果は、.class を取り除き、ファイルパスを、すべてドット(.)に変換した形式にします。
035 * これは、ほとんどクラスのフルパス文字列に相当します。
036 * ここで取得されたファイル名より、実クラスオブジェクトの作成が可能になります。
037 *
038 * このクラスの main メソッドは、クラスパスから指定の名前を持つクラス以下のディレクトリより
039 * ファイルを検索します。通常、このクラスの使い方として、取得したクラスファイル名(文字列)
040 * から、引数なしコンストラクタを呼び出して、実オブジェクトを生成させるので、通常のフォルダ
041 * から検索するより、クラスパス内から検索するペースが多いため、サンプルをそのように設定
042 * しています。
043 *
044 * @og.rev 4.0.0.0 (2004/12/31) 新規作成
045 * @og.group 初期化
046 *
047 * @version  4.0
048 * @author   Kazuhiko Hasegawa
049 * @since    JDK5.0,
050 */
051public final class FindClassFiles {
052        private final List<String> list = new ArrayList<>();
053        private final int    baseLen;
054
055        private static final String SUFIX     = ".class" ;
056        private static final int    SUFIX_LEN = SUFIX.length();
057
058        /**
059         * 検索パスを指定して構築する、コンストラクタです。
060         * ここで見つかったパス以下の classファイル(拡張子は小文字で、.class )を検索します。
061         * このファイル名は ファイルパスを ドット(.)に置き換え、.class を取り除いた格納しておきます。
062         *
063         * ※ Tomcat8.0.3 では、ClassLoader の getResources(String)で取得するURL名が、
064         *    /C:/opengionV6/uap/webapps/gf/WEB-INF/classes/org/opengion/plugin/
065         *    の形式で、最後の "/" を取る為、filepath.length() - 1 処理していましたが、
066         *    Tomcat8.0.5 では、/C:/opengionV6/uap/webapps/gf/WEB-INF/classes/org/opengion/plugin
067         *    の形式で、最後の "/" がなくなっています。
068         *    最後の "/" があってもなくても、new File(String) でディレクトリのオブジェクトを
069         *    作成できるため、filepath.length() に変更します。
070         *
071         * @og.rev 4.0.3.0 (2007/01/07) UNIXパス検索時の、ファイルパスの取得方法の不具合対応
072         * @og.rev 5.0.0.0 (2009/08/03) UNIXパス検索時の、ファイルパスの取得方法の不具合対応
073         * @og.rev 5.0.0.0 (2009/08/03) UNIXパス検索時の、ファイルパスの取得方法の不具合対応
074         * @og.rev 5.7.5.0 (2014/04/04) ファイル名の取得方法の修正
075         *
076         * @param       filepath        対象となるファイル群を検索する、ファイルパス
077         * @param       keyword         検索対象ファイルのキーワード
078         */
079        public FindClassFiles( final String filepath,final String keyword ) {
080                // jar:file:/実ディレクトリ!先頭クラス または、file:/実ディレクトリ または、/実ディレクトリ
081
082                String dir = filepath;                                                  // 5.5.2.6 (2012/05/25) findbugs対応
083                if( filepath.startsWith( "jar:" )
084                                        || filepath.startsWith( "file:" )
085                                        || filepath.charAt(0) == '/' ) {                // 4.4.0.0 (2009/08/02)
086                        int stAdrs = filepath.indexOf( '/' );
087                        if( filepath.charAt(stAdrs+2) == ':' ) {        // 4.0.3.0 (2007/01/07)
088                                stAdrs++;
089                        }
090                        int edAdrs = filepath.lastIndexOf( '!' );
091                        if( edAdrs < 0) {
092                                edAdrs = filepath.length();                     // 5.7.5.0 (2014/04/04) 最後の "/" はあってもなくてもよい。
093                        }
094                        dir = filepath.substring( stAdrs,edAdrs );
095                }
096
097                final File basefile = new File( dir );
098                final String baseFilename = basefile.getAbsolutePath() ;
099                baseLen = baseFilename.length() - keyword.length();
100                findFilename( basefile );
101        }
102
103        /**
104         * ファイルパスを ドット(.)に置き換え、.class を取り除いた形式(クラスの完全系)の文字列配列。
105         *
106         * @return      ファイルパスの文字列配列
107         * @og.rtnNotNull
108         */
109        public String[] getFilenames() {
110                return list.toArray( new String[list.size()] );
111        }
112
113        /**
114         * ファイルを再帰的に検索します。
115         * 指定のファイルオブジェクトが ファイルの場合は、.class であればListに追加し、
116         * .zip か .jar では、findJarFiles を呼び出します。
117         * ファイルでない場合は、配列を取り出し、自分自身を再帰的に呼び出します。
118         *
119         * @og.rev 6.3.9.0 (2015/11/06) null になっている可能性があるメソッドの戻り値のnullチェックを追加。
120         *
121         * @param       file    ファイル
122         */
123        private void findFilename( final File file ) {
124                if( file.isFile() ) {
125                        final String name = file.getAbsolutePath();
126                        if( name.endsWith( SUFIX ) ) {
127                                list.add( name.substring( baseLen,name.length()-SUFIX_LEN ).replace( File.separatorChar,'.' ) );
128                        }
129                        else if( name.endsWith( ".jar" ) || name.endsWith( ".zip" ) ) {
130                                findJarFiles( name );
131                        }
132                }
133                else {
134                        final File[] filelist = file.listFiles();
135                        // 6.3.9.0 (2015/11/06) null になっている可能性があるメソッドの戻り値のnullチェックを追加。
136                        if( filelist != null ) {
137                                for( int i=0; i<filelist.length; i++ ) {
138                                        findFilename( filelist[i] );
139                                }
140                        }
141                }
142        }
143
144        /**
145         * jar/zipファイルを検索します。
146         * 圧縮ファイルでは、階層ではなく、Enumeration としてファイルを取り出します。
147         * 拡張子で判断して、Listに追加していきます。
148         *
149         * @og.rev 5.5.2.6 (2012/05/25) JarFile を、Closer#zipClose( ZipFile ) メソッドを利用して、close します。
150         *
151         * @param       filename        ファイル名
152         */
153        private void findJarFiles( final String filename ) {
154                // 5.5.2.6 (2012/05/25) findbugs対応
155                JarFile jarFile = null;
156                try {
157                        jarFile = new JarFile( filename );
158                        final Enumeration<JarEntry> en = jarFile.entries() ;
159                        while( en.hasMoreElements() ) {
160                                final JarEntry ent = en.nextElement();
161                                if( ! ent.isDirectory() ) {
162                                        final String name = ent.getName();
163                                        if( name.endsWith( SUFIX ) ) {
164                                                list.add( name.substring( 0,name.length()-SUFIX_LEN ).replace( '/','.' ) );
165                                        }
166                                }
167                        }
168                }
169                catch( final IOException ex ) {
170                        final String errMsg = "ファイル読み取りストリームに失敗しました。"
171                                        + " File=" + filename
172                                        + ex.getMessage();                              // 5.1.8.0 (2010/07/01) errMsg 修正
173                        throw new OgRuntimeException( errMsg,ex );
174                }
175                // 5.5.2.6 (2012/05/25) findbugs対応
176                finally {
177                        Closer.zipClose( jarFile );
178                }
179        }
180
181        /**
182         * サンプルメイン
183         * ここでは、引数に通常のファイルではなく、クラスパスより取得します。
184         * 通常、取得されたファイル名は、クラスの完全系の文字列なので、クラスパスより取得
185         * している限り、そのまま オブジェクトを構築できることを意味します。
186         *
187         * @og.rev 6.8.5.1 (2018/01/15) ファイル名は、##バージョン番号を変換しておく必要がある。
188         *
189         * @param       args    引数
190         */
191        public static void main( final String[] args ) {
192                try {
193                        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
194                        final Enumeration<URL> enume = loader.getResources( args[0] );  // 4.3.3.6 (2008/11/15) Generics警告対応
195                        while( enume.hasMoreElements() ) {
196                                final URL url = enume.nextElement();            // 4.3.3.6 (2008/11/15) Generics警告対応
197                                // jar:file:/実ディレクトリ!先頭クラス または、file:/実ディレクトリ
198                                final String dir = url.getFile().replaceAll( "%23%23","##" );           // 6.8.5.1 (2018/01/15)
199//                              System.out.println( "url=" + url.getFile() );
200                                System.out.println( "url=" + dir );                                                                     // 6.8.5.1 (2018/01/15)
201
202//                              final FindClassFiles filenames = new FindClassFiles( url.getFile(),args[0] );
203                                final FindClassFiles filenames = new FindClassFiles( dir,args[0] );     // 6.8.5.1 (2018/01/15)
204                                final String[] names = filenames.getFilenames();
205                                for( int i=0; i<names.length; i++ ) {
206                                        System.out.println( names[i] );
207                                }
208                        }
209                }
210                catch( final IOException ex ) {
211                        final String errMsg = "ファイル読み取りストリームに失敗しました。"
212                                        + ex.getMessage();
213                        throw new OgRuntimeException( errMsg,ex );
214                }
215        }
216}