1 | package de.uka.ipd.sdq.dsexplore.helper; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.Iterator; |
5 | import java.util.List; |
6 | |
7 | import org.apache.log4j.Logger; |
8 | import org.eclipse.core.runtime.CoreException; |
9 | import org.eclipse.debug.core.ILaunchConfiguration; |
10 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
11 | |
12 | import de.uka.ipd.sdq.simucomframework.SimuComConfig; |
13 | import de.uka.ipd.sdq.workflow.pcm.ConstantsContainer; |
14 | |
15 | /** |
16 | * helps handling the {@link ILaunchConfiguration} needed during a Design Exploration. Offers methods to generate new {@link ILaunchConfiguration} to run the simulation and also delete the generated {@link ILaunchConfiguration}s again. |
17 | * |
18 | * The {@link ConfigurationHelper} is globally available as a singleton instance, you can also create a new {@link ConfigurationHelper} for local use. |
19 | * @author Anne |
20 | * |
21 | */ |
22 | public class ConfigurationHelper { |
23 | |
24 | /** Logger for log4j. */ |
25 | private static Logger logger = |
26 | Logger.getLogger("de.uka.ipd.sdq.dsexplore"); |
27 | |
28 | /** |
29 | * Singleton instance |
30 | */ |
31 | private static ConfigurationHelper helper = new ConfigurationHelper(); |
32 | |
33 | /** |
34 | * Remembers the {@link ILaunchConfiguration} this Helper generated so that it can delete them again. |
35 | */ |
36 | private List<ILaunchConfiguration> rememberedLaunchConfigs = new ArrayList<ILaunchConfiguration>(); |
37 | |
38 | /** |
39 | * Creates a copy of the passed {@link ILaunchConfiguration} and updates the file names of the passed {@link PCMInstance}. |
40 | * @param configuration The base configuration to copy from |
41 | * @param instance the {@link PCMInstance} to use the file names from. |
42 | * @return a copy of configuration in which the filenames of the instance are used. |
43 | * @throws CoreException if the configuration cannot be copied or the new configuration cannot be saved to the run dialogs |
44 | */ |
45 | public ILaunchConfiguration updateConfig(final ILaunchConfiguration configuration) throws CoreException { |
46 | |
47 | |
48 | ILaunchConfigurationWorkingCopy workingCopy = configuration.copy(configuration.getName()+"-c"); |
49 | copyAndMarkAttribute(ConstantsContainer.ALLOCATION_FILE, configuration, workingCopy); |
50 | copyAndMarkAttribute(ConstantsContainer.SYSTEM_FILE, configuration, workingCopy); |
51 | copyAndMarkAttribute(ConstantsContainer.USAGE_FILE, configuration, workingCopy); |
52 | |
53 | //workingCopy.setAttribute(ConstantsContainer.FEATURE_CONFIG, instance.getConnectorConfigFilename()); |
54 | |
55 | workingCopy.setAttribute(SimuComConfig.EXPERIMENT_RUN, configuration.getName()+"-c"); |
56 | |
57 | workingCopy.setAttribute(SimuComConfig.SHOULD_THROW_EXCEPTION, false); |
58 | |
59 | ILaunchConfiguration newLaunchConfig = workingCopy.doSave(); |
60 | |
61 | this.rememberedLaunchConfigs.add(newLaunchConfig); |
62 | |
63 | return newLaunchConfig; |
64 | |
65 | } |
66 | |
67 | public void copyAndMarkAttribute(String id, ILaunchConfiguration original, ILaunchConfigurationWorkingCopy workingCopy) throws CoreException{ |
68 | String suffix = "-c"; |
69 | String fileName = original.getAttribute(id, ""); |
70 | String newFileName = appendToFilename(suffix, fileName); |
71 | workingCopy.setAttribute(id, newFileName); |
72 | |
73 | /*//copy file |
74 | File in = new File(fileName); |
75 | File out = new File(newFileName); |
76 | |
77 | |
78 | FileChannel inChannel; |
79 | try { |
80 | inChannel = new FileInputStream(in).getChannel(); |
81 | } catch (FileNotFoundException e1) { |
82 | throw new CoreException(new Status(Status.ERROR, "de.uka.ipd.sdq.dsexplore", 0, "PCM model file could not be accessed: "+fileName+".", e1)); |
83 | } |
84 | |
85 | FileChannel outChannel; |
86 | try { |
87 | outChannel = new FileOutputStream(out).getChannel(); |
88 | } catch (FileNotFoundException e1) { |
89 | throw new CoreException(new Status(Status.ERROR, "de.uka.ipd.sdq.dsexplore", 0, "New PCM model file could not be accessed: "+newFileName+".", e1)); |
90 | } |
91 | |
92 | try { |
93 | inChannel.transferTo(0, inChannel.size(), |
94 | outChannel); |
95 | } |
96 | catch (IOException e) { |
97 | throw new CoreException(new Status(Status.ERROR, "de.uka.ipd.sdq.dsexplore", 0, "PCM model file could not be copied: "+fileName+" to "+newFileName+".", e)); |
98 | } |
99 | finally { |
100 | if (inChannel != null) |
101 | try { |
102 | inChannel.close(); |
103 | } catch (IOException e) { |
104 | throw new CoreException(new Status(Status.ERROR, "de.uka.ipd.sdq.dsexplore", 0, "Could not close PCM model file: "+fileName+".", e)); |
105 | } |
106 | if (outChannel != null) |
107 | try { |
108 | outChannel.close(); |
109 | } catch (IOException e) { |
110 | throw new CoreException(new Status(Status.ERROR, "de.uka.ipd.sdq.dsexplore", 0, "Could not close new PCM model file: "+newFileName+".", e)); |
111 | } |
112 | }*/ |
113 | } |
114 | |
115 | |
116 | |
117 | private String appendToFilename(String fileNameSuffix, String fileName) { |
118 | int indexOfLastDot = fileName.lastIndexOf("."); |
119 | |
120 | if (indexOfLastDot < 1){ |
121 | return fileName + fileNameSuffix; |
122 | } else { |
123 | return fileName.substring(0, indexOfLastDot) + fileNameSuffix |
124 | + fileName.substring(indexOfLastDot); |
125 | } |
126 | } |
127 | |
128 | /** |
129 | * Deletes all {@link ILaunchConfiguration} this helper has generated before. |
130 | */ |
131 | public void cleanUp(){ |
132 | for (Iterator<ILaunchConfiguration> iterator = rememberedLaunchConfigs.iterator(); iterator |
133 | .hasNext();) { |
134 | ILaunchConfiguration config = iterator.next(); |
135 | try { |
136 | config.delete(); |
137 | } catch (CoreException e) { |
138 | logger.warn("Could not clean up the launch configs, thus some will stay in your run dialog."); |
139 | e.printStackTrace(); |
140 | } |
141 | } |
142 | } |
143 | |
144 | /** |
145 | * Get the global singleton instance of this helper. |
146 | * @return |
147 | */ |
148 | public static ConfigurationHelper getInstance(){ |
149 | return helper; |
150 | } |
151 | |
152 | |
153 | |
154 | |
155 | } |