View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.container.convention;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import org.eclipse.core.runtime.FileLocator;
26  import org.seasar.framework.convention.NamingConvention;
27  import org.seasar.framework.convention.impl.NamingConventionImpl;
28  import org.seasar.framework.exception.IORuntimeException;
29  import org.seasar.framework.util.ClassLoaderUtil;
30  import org.seasar.framework.util.Disposable;
31  import org.seasar.framework.util.StringUtil;
32  import org.seasar.framework.util.URLUtil;
33  
34  /**
35   * {@link NamingConvention}の実装クラスです。
36   * 
37   * @author y.sugigami
38   */
39  public class UrumaNamingConventionImpl extends NamingConventionImpl implements
40          NamingConvention, Disposable {
41  
42      /**
43       * {@link NamingConventionImpl}を作成します。
44       */
45      public UrumaNamingConventionImpl() {
46          super();
47      }
48  
49      /**
50       * 存在チェッカの配列を作成します。
51       * 
52       * @param rootPackageName
53       *            ルートパッケージ名
54       * @return 存在チェッカの配列
55       */
56      @Override
57      @SuppressWarnings("unchecked")
58      protected ExistChecker[] createExistCheckerArray(
59              final String rootPackageName) {
60          if (StringUtil.isEmpty(rootPackageName)) {
61              return new ExistChecker[0];
62          }
63          final String s = rootPackageName.replace('.', '/');
64          final List<ExistChecker> list = new ArrayList<ExistChecker>();
65          for (final Iterator<URL> it = ClassLoaderUtil.getResources(this
66                  .getClass(), s); it.hasNext();) {
67              final URL url = it.next();
68              final String protocol = URLUtil.toCanonicalProtocol(url
69                      .getProtocol());
70              if ("file".equals(protocol)) {
71                  list.add(new FileExistChecker(url));
72              } else if ("jar".equals(protocol)) {
73                  list.add(new JarExistChecker(url, rootPackageName));
74              } else if ("zip".equals(protocol)) {
75                  list.add(new ZipExistChecker(url, rootPackageName));
76              } else if ("code-source".equals(protocol)) {
77                  list.add(new CodeSourceExistChecker(url, rootPackageName));
78              } else if ("bundleresource".equals(protocol)) {
79                  list.add(new BundleExistChecker(url));
80              }
81          }
82          return list.toArray(new ExistChecker[list.size()]);
83      }
84  
85      /**
86       * バンドル用の存在チェッカです。
87       * 
88       */
89      protected static class BundleExistChecker implements ExistChecker {
90          private File rootFile;
91  
92          /**
93           * インスタンスを作成します。
94           * 
95           * @param rootUrl
96           *            ルートURL
97           */
98          protected BundleExistChecker(final URL rootUrl) {
99              URL fileLocatorUrl = null;
100             try {
101                 fileLocatorUrl = FileLocator.toFileURL(rootUrl);
102             } catch (IOException ex) {
103                 throw new IORuntimeException(ex);
104             }
105             if (fileLocatorUrl != null) {
106                 rootFile = new File(fileLocatorUrl.getPath());
107             }
108 
109         }
110 
111         public boolean isExist(final String lastClassName) {
112             final File file = new File(rootFile, getPathName(lastClassName));
113             return file.exists();
114         }
115 
116         public void close() {
117         }
118     }
119 
120 }