View Javadoc

1   /*
2    * Copyright 2005 Jukka Zitting <jz@yukatan.fi>
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.exorcist.core;
17  
18  import java.io.FileInputStream;
19  import java.io.IOException;
20  import java.lang.reflect.InvocationTargetException;
21  import java.util.Enumeration;
22  import java.util.Properties;
23  
24  import org.apache.commons.beanutils.BeanUtils;
25  import org.springframework.beans.BeansException;
26  import org.springframework.context.ApplicationContext;
27  import org.springframework.context.support.ClassPathXmlApplicationContext;
28  import org.springframework.context.support.FileSystemXmlApplicationContext;
29  
30  import net.sf.exorcist.api.ContentConverter;
31  import net.sf.exorcist.api.ContentException;
32  import net.sf.exorcist.api.ContentExporter;
33  import net.sf.exorcist.api.ContentImporter;
34  import net.sf.exorcist.api.ContentState;
35  import net.sf.exorcist.core.memory.MemoryContentState;
36  
37  /***
38   * The Exorcist main class.
39   */
40  public class Exorcist {
41  
42      private static final String CONTEXT_XML = "net/sf/exorcist/core/context.xml";
43      /*** The content state. */
44      private final ContentState state;
45  
46      public Exorcist() {
47          state = new MemoryContentState();
48      }
49  
50      private static Properties getProperties(String key)
51              throws IOException {
52          Properties properties = new Properties();
53          properties.load(new FileInputStream(System.getProperty(key, key)));
54          return properties;
55      }
56  
57      private static Object instantiate(Properties properties)
58              throws ContentException {
59          try {
60              Object object =
61                  Class.forName(properties.getProperty("class")).newInstance();
62              Enumeration names = properties.propertyNames();
63              while (names.hasMoreElements()) {
64                  String name = (String) names.nextElement();
65                  if (!name.equals("class")) {
66                      BeanUtils.setProperty(
67                              object, name, properties.getProperty(name));
68                  }
69              }
70              return object;
71          } catch (InvocationTargetException e) {
72              throw new ContentException(e);
73          } catch (InstantiationException e) {
74              throw new ContentException(e);
75          } catch (IllegalAccessException e) {
76              throw new ContentException(e);
77          } catch (ClassNotFoundException e) {
78              throw new ContentException(e);
79          }
80      }
81  
82      public void importContent(Properties properties) throws ContentException {
83          try {
84              ContentImporter importer = (ContentImporter) instantiate(properties);
85              importer.importContent(state);
86          } catch (ClassCastException e) {
87              throw new ContentException(e);
88          }
89      }
90  
91      public void exportContent(Properties properties) throws ContentException {
92          try {
93              ContentExporter exporter = (ContentExporter) instantiate(properties);
94              exporter.exportContent(state);
95          } catch (ClassCastException e) {
96              throw new ContentException(e);
97          }
98      }
99  
100     public void convertContent(Properties properties) throws ContentException {
101         try {
102             ContentConverter converter = (ContentConverter) instantiate(properties);
103             converter.convertContent(state);
104         } catch (ClassCastException e) {
105             throw new ContentException(e);
106         }
107     }
108 
109     public static void main(String[] args) {
110         try {
111             ApplicationContext rootContext =
112                 new ClassPathXmlApplicationContext(CONTEXT_XML);
113             ApplicationContext context =
114                 new FileSystemXmlApplicationContext(args, rootContext);
115 
116             ContentState state = (ContentState)
117                 context.getBean("state", ContentState.class);
118             ContentExporter exporter = (ContentExporter)
119                 context.getBean("exporter", ContentExporter.class);
120             ContentConverter converter = (ContentConverter)
121                 context.getBean("converter", ContentConverter.class);
122             ContentImporter importer = (ContentImporter)
123                 context.getBean("importer", ContentImporter.class);
124 
125             exporter.exportContent(state);
126             converter.convertContent(state);
127             importer.importContent(state);
128 
129             System.exit(0);
130         } catch (BeansException e) {
131             e.printStackTrace();
132             System.exit(1);
133         } catch (ContentException e) {
134             e.printStackTrace();
135             System.exit(1);
136         }
137     }
138     
139 }