View Javadoc

1   /*
2    * $Id: PlexusUtils.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.plexus;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.InputStream;
26  import java.io.InputStreamReader;
27  
28  import org.codehaus.plexus.PlexusContainer;
29  import org.codehaus.plexus.configuration.PlexusConfigurationResourceException;
30  
31  import com.opensymphony.xwork2.util.logging.Logger;
32  import com.opensymphony.xwork2.util.logging.LoggerFactory;
33  
34  /***
35   * Utility methods for dealing with Plexus
36   */
37  public class PlexusUtils {
38      private static final Logger LOG = LoggerFactory.getLogger(PlexusObjectFactory.class);
39  
40      /***
41       * Configures the container with the configuration file
42       *
43       * @param pc The plexus container
44       * @param file The file path
45       * @throws PlexusConfigurationResourceException If the plexus configuration can't be loaded
46       */
47      public static void configure(PlexusContainer pc, String file) throws PlexusConfigurationResourceException {
48          InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
49          if (is == null) {
50              LOG.info("Could not find " + file + ", skipping");
51              is = new ByteArrayInputStream("<plexus><components></components></plexus>".getBytes());
52          }
53          pc.setConfigurationResource(new InputStreamReader(is));
54      }
55  }