1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package org.jomc.cli.commands;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.Locale;
36 import java.util.logging.Level;
37 import javax.xml.bind.JAXBContext;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Marshaller;
40 import javax.xml.bind.util.JAXBSource;
41 import javax.xml.transform.Source;
42 import org.apache.commons.cli.CommandLine;
43 import org.jomc.model.Implementation;
44 import org.jomc.model.Module;
45 import org.jomc.model.Specification;
46 import org.jomc.modlet.Model;
47 import org.jomc.modlet.ModelContext;
48 import org.jomc.modlet.ModelException;
49 import org.jomc.modlet.ModelValidationReport;
50 import org.jomc.modlet.ObjectFactory;
51 import org.jomc.tools.SourceFileProcessor;
52
53
54
55
56
57
58 public final class ManageSourcesCommand extends AbstractSourceFileProcessorCommand
59 {
60
61
62
63
64 public ManageSourcesCommand()
65 {
66 super();
67 }
68
69 public String getName()
70 {
71 return "manage-sources";
72 }
73
74 public String getAbbreviatedName()
75 {
76 return "ms";
77 }
78
79 public String getShortDescription( final Locale locale )
80 {
81 return Messages.getMessage( "manageSourcesShortDescription" );
82 }
83
84 public String getLongDescription( final Locale locale )
85 {
86 return null;
87 }
88
89 protected void processSourceFiles( final CommandLine commandLine ) throws CommandExecutionException
90 {
91 if ( commandLine == null )
92 {
93 throw new NullPointerException( "commandLine" );
94 }
95
96 CommandLineClassLoader classLoader = null;
97
98 try
99 {
100 classLoader = new CommandLineClassLoader( commandLine );
101 final ModelContext context = this.createModelContext( commandLine, classLoader );
102 final Model model = this.getModel( context, commandLine );
103 final JAXBContext jaxbContext = context.createContext( model.getIdentifier() );
104 final Marshaller marshaller = context.createMarshaller( model.getIdentifier() );
105 final Source source = new JAXBSource( jaxbContext, new ObjectFactory().createModel( model ) );
106 final ModelValidationReport validationReport = context.validateModel( model.getIdentifier(), source );
107 this.log( validationReport, marshaller );
108
109 if ( !validationReport.isModelValid() )
110 {
111 throw new CommandExecutionException( Messages.getMessage( "invalidModel",
112 this.getModel( commandLine ) ) );
113
114 }
115
116 final SourceFileProcessor tool = this.createSourceFileProcessor( commandLine );
117 tool.setModel( model );
118
119 final File sourcesDirectory =
120 new File( commandLine.getOptionValue( Options.SOURCE_DIRECTORY_OPTION.getOpt() ) );
121
122 final Specification specification = this.getSpecification( commandLine, model );
123 final Implementation implementation = this.getImplementation( commandLine, model );
124 final Module module = this.getModule( commandLine, model );
125
126 if ( specification != null )
127 {
128 tool.manageSourceFiles( specification, sourcesDirectory );
129 }
130
131 if ( implementation != null )
132 {
133 tool.manageSourceFiles( implementation, sourcesDirectory );
134 }
135
136 if ( module != null )
137 {
138 tool.manageSourceFiles( module, sourcesDirectory );
139 }
140
141 if ( this.isModulesProcessingRequested( commandLine ) )
142 {
143 tool.manageSourceFiles( sourcesDirectory );
144 }
145
146 classLoader.close();
147 classLoader = null;
148 }
149 catch ( final JAXBException e )
150 {
151 String message = Messages.getMessage( e );
152 if ( message == null )
153 {
154 message = Messages.getMessage( e.getLinkedException() );
155 }
156
157 throw new CommandExecutionException( message, e );
158 }
159 catch ( final ModelException e )
160 {
161 throw new CommandExecutionException( Messages.getMessage( e ), e );
162 }
163 catch ( final IOException e )
164 {
165 throw new CommandExecutionException( Messages.getMessage( e ), e );
166 }
167 finally
168 {
169 try
170 {
171 if ( classLoader != null )
172 {
173 classLoader.close();
174 }
175 }
176 catch ( final IOException e )
177 {
178 this.log( Level.SEVERE, Messages.getMessage( e ), e );
179 }
180 }
181 }
182
183 }