EMMA Coverage Report (generated Sun Feb 05 10:43:15 CET 2012)
[all classes][de.uka.ipd.sdq.dsexplore.helper]

COVERAGE SUMMARY FOR SOURCE FILE [ConfigurationHelper.java]

nameclass, %method, %block, %line, %
ConfigurationHelper.java0%   (0/1)0%   (0/7)0%   (0/142)0%   (0/33)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ConfigurationHelper0%   (0/1)0%   (0/7)0%   (0/142)0%   (0/33)
<static initializer> 0%   (0/1)0%   (0/8)0%   (0/4)
ConfigurationHelper (): void 0%   (0/1)0%   (0/8)0%   (0/2)
appendToFilename (String, String): String 0%   (0/1)0%   (0/32)0%   (0/5)
cleanUp (): void 0%   (0/1)0%   (0/22)0%   (0/8)
copyAndMarkAttribute (String, ILaunchConfiguration, ILaunchConfigurationWorki... 0%   (0/1)0%   (0/17)0%   (0/5)
getInstance (): ConfigurationHelper 0%   (0/1)0%   (0/2)0%   (0/1)
updateConfig (ILaunchConfiguration): ILaunchConfiguration 0%   (0/1)0%   (0/53)0%   (0/9)

1package de.uka.ipd.sdq.dsexplore.helper;
2 
3import java.util.ArrayList;
4import java.util.Iterator;
5import java.util.List;
6 
7import org.apache.log4j.Logger;
8import org.eclipse.core.runtime.CoreException;
9import org.eclipse.debug.core.ILaunchConfiguration;
10import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
11 
12import de.uka.ipd.sdq.simucomframework.SimuComConfig;
13import 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 */
22public 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}

[all classes][de.uka.ipd.sdq.dsexplore.helper]
EMMA 2.0.9414 (unsupported private build) (C) Vladimir Roubtsov