diff --git a/Kieker.WebGUI/.settings/org.eclipse.jdt.core.prefs b/Kieker.WebGUI/.settings/org.eclipse.jdt.core.prefs index 8a7bab01b61779177af532e0fca1fa1f1360153b..4ad575de23b8bac76a80d2967d5bb72af23f4ece 100644 --- a/Kieker.WebGUI/.settings/org.eclipse.jdt.core.prefs +++ b/Kieker.WebGUI/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,4 @@ -#Sun Jan 22 11:49:37 CET 2012 +#Thu Jan 26 11:46:13 CET 2012 eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 org.eclipse.jdt.core.compiler.compliance=1.6 diff --git a/Kieker.WebGUI/.settings/org.eclipse.jdt.ui.prefs b/Kieker.WebGUI/.settings/org.eclipse.jdt.ui.prefs index 213ac9158829015b0160c9b97b2e05842d53dd0a..9d66836fd3a927eb12d0328381a0698e606f8378 100644 --- a/Kieker.WebGUI/.settings/org.eclipse.jdt.ui.prefs +++ b/Kieker.WebGUI/.settings/org.eclipse.jdt.ui.prefs @@ -1,4 +1,4 @@ -#Sun Jan 22 11:49:37 CET 2012 +#Thu Jan 26 11:46:13 CET 2012 cleanup.add_default_serial_version_id=true cleanup.add_generated_serial_version_id=false cleanup.add_missing_annotations=true diff --git a/Kieker.WebGUI/lib/kieker-1.5-SNAPSHOT.jar b/Kieker.WebGUI/lib/kieker-1.5-SNAPSHOT.jar index 2daa858bf513784fd899779bc0b298bccd79b7da..d90af68674b225ef94f03774593d1322a9490c85 100644 Binary files a/Kieker.WebGUI/lib/kieker-1.5-SNAPSHOT.jar and b/Kieker.WebGUI/lib/kieker-1.5-SNAPSHOT.jar differ diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/AvailableProjectsBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/AvailableProjectsBean.java index 3f849ce11b3d74aa16e2bd1b2b38fa4bfa8f568d..a5af7e44be766f6ab8c64fb6d8fad459ea490ace 100644 --- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/AvailableProjectsBean.java +++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/AvailableProjectsBean.java @@ -1,6 +1,5 @@ package kieker.webgui.beans; -import java.io.File; import java.util.ArrayList; import java.util.List; @@ -8,68 +7,62 @@ import javax.faces.bean.ApplicationScoped; import javax.faces.bean.ManagedBean; import javax.faces.context.FacesContext; -import kieker.analysis.AnalysisController; import kieker.analysis.model.analysisMetaModel.MIProject; import kieker.analysis.model.analysisMetaModel.impl.MAnalysisMetaModelFactory; +import kieker.webgui.common.FileManager; import org.primefaces.model.DefaultTreeNode; import org.primefaces.model.TreeNode; /** * This bean can be used to handle all available projects within the program. - * + * * @author Nils Christian Ehmke */ @ManagedBean @ApplicationScoped public class AvailableProjectsBean { - private final String PROJECT_DIR = "projects"; - private final List<MIProject> projects; - private final MAnalysisMetaModelFactory factory; + private final List<MIProject> projects; + private final MAnalysisMetaModelFactory factory; - public AvailableProjectsBean() { - projects = new ArrayList<MIProject>(); - factory = new MAnalysisMetaModelFactory(); - } + public AvailableProjectsBean() { + projects = FileManager.getInstance().loadAllProjects(); + factory = new MAnalysisMetaModelFactory(); + } - public synchronized boolean addProject(final String projectName) { - final MIProject project = factory.createProject(); - project.setName(projectName); + public synchronized boolean addProject(final String projectName) { + /* Create a new project. */ + final MIProject project = factory.createProject(); + project.setName(projectName); - try { - File dir = new File(PROJECT_DIR + "/" + projectName); - if (!dir.exists()) { - dir.mkdir(); - } else { - /* The project exists already. */ - return false; - } - File f = new File(dir + "/" + projectName + ".xml"); - new AnalysisController(project).saveToFile(f, projectName); - } catch (Exception ex) { - System.out.println(ex); - } + /* Try to save the project. */ + if (!FileManager.getInstance().saveNewProject(project)) { + return false; + } else { - FacesContext context = FacesContext.getCurrentInstance(); - SelectedProjectBean bean = (SelectedProjectBean) context.getApplication().evaluateExpressionGet(context, "#{selectedProjectBean}", SelectedProjectBean.class); - if (bean.getMainProject() == null) { - bean.setMainProject(project); - } + /* Set the new project as the main project, if the current user doesn't have one. */ + FacesContext context = FacesContext.getCurrentInstance(); + SelectedProjectBean bean = (SelectedProjectBean) context.getApplication().evaluateExpressionGet(context, "#{selectedProjectBean}", + SelectedProjectBean.class); + if (bean.getMainProject() == null) { + bean.setMainProject(project); + } - return projects.add(project); - } + return projects.add(project); + } + } - @SuppressWarnings(value = { "unused" }) - public synchronized TreeNode getProjectsRoot() { - TreeNode root = new DefaultTreeNode("Root", null); + @SuppressWarnings(value = { "unused" }) + public synchronized TreeNode getProjectsRoot() { + TreeNode root = new DefaultTreeNode("Root", null); - for (final MIProject project : projects) { - final TreeNode projectNode = new DefaultTreeNode("project", project, root); - final TreeNode dependenciesNode = new DefaultTreeNode("dependencies", "Dependencies", projectNode); - final TreeNode usedPluginsNode = new DefaultTreeNode("usedPlugins", "Used Plugins", projectNode); - } + for (final MIProject project : projects) { + final TreeNode projectNode = new DefaultTreeNode("project", project, root); + final TreeNode dependenciesNode = new DefaultTreeNode("dependencies", "Dependencies", projectNode); + final TreeNode usedPluginsNode = new DefaultTreeNode("usedPlugins", "Used Plugins", projectNode); + } - return root; - } + return root; + } } diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/common/FileManager.java b/Kieker.WebGUI/src/main/java/kieker/webgui/common/FileManager.java new file mode 100644 index 0000000000000000000000000000000000000000..ba3188265fe5b0992e2bd823d46e90a310465086 --- /dev/null +++ b/Kieker.WebGUI/src/main/java/kieker/webgui/common/FileManager.java @@ -0,0 +1,139 @@ +package kieker.webgui.common; + +import java.io.File; +import java.io.FileFilter; +import java.util.ArrayList; +import java.util.List; + +import kieker.analysis.AnalysisController; +import kieker.analysis.model.analysisMetaModel.MIProject; +import kieker.common.logging.Log; +import kieker.common.logging.LogFactory; + +/** + * This is a singleton class for the access to the file system. It makes sure that the necessary directories for the execution of the application exist. <b>Do + * not</b> remove directories created from this manager during runtime! Directories are created during first access to the class.<br> + * Currently nearly all methods are synchronized. A fine grained synchronization should be implemented in the future. + * + * @author Nils Christian Ehmke + */ +public final class FileManager { + + private static final Log LOG = LogFactory.getLog(FileManager.class); + + private static final String ROOT_DIR = "data"; + private static final String PROJECT_DIR = ROOT_DIR + File.separator + "projects"; + private static final String LIB_DIR = ROOT_DIR + File.separator + "libraries"; + private static final String EXTENSION = ".xml"; + private static final FileManager instance = new FileManager(); + + private FileManager() { + checkAndCreateDirectories(); + } + + private synchronized void checkAndCreateDirectories() { + /* + * Make sure that the directories exist and create them if necessary. + */ + final File dirProj = new File(PROJECT_DIR); + final File dirLib = new File(LIB_DIR); + boolean couldCreated = true; + + if (!dirProj.exists()) { + couldCreated &= dirProj.mkdirs(); + } + if (!dirLib.exists()) { + couldCreated &= dirLib.mkdirs(); + } + + if (!couldCreated) { + FileManager.LOG.error("Could not create the necessary directories for the application"); + } + } + + /** + * This method saves a given project using the name of the project as the project-directory. The project-directory must already exist. + * + * @param project + * The project to be stored. + * @return true iff the project-directory does already exist and the storage was successful. + */ + public synchronized boolean saveProject(final MIProject project) { + final String projectName = project.getName(); + + final File dirProject = new File(PROJECT_DIR + File.separator + projectName); + + /* Make sure that the directory for the project exists. */ + if (!dirProject.exists()) { + return false; + } else { + /* Try to save the project. */ + final File fileProject = new File(dirProject, projectName + FileManager.EXTENSION); + try { + final AnalysisController controller = new AnalysisController(project); + return controller.saveToFile(fileProject, projectName); + } catch (final Exception ex) { + FileManager.LOG.error("Could not save project '" + projectName + "'."); + return false; + } + } + } + + /** + * This method saves a given project using the name of the project as the project-directory. THe project-directory must not already exist. + * + * @param project + * The project to be stored. + * @return true iff the project-directory does not already exist and the storage was successful. + */ + public synchronized boolean saveNewProject(final MIProject project) { + final String projectName = project.getName(); + + final File dirProject = new File(PROJECT_DIR + File.separator + projectName); + + /* Make sure that the project does not already exist and create the project directory. */ + if (dirProject.exists() || !dirProject.mkdir()) { + return false; + } else { + /* The directory should exist now. Store the project. */ + return saveProject(project); + } + } + + /** + * This method can be used to load all currently saved projects. + * + * @return A list containing the loaded projects. If something went wrong, an empty list will be returned, never null. + */ + public synchronized List<MIProject> loadAllProjects() { + List<MIProject> resultList = new ArrayList<MIProject>(); + + /* Try to get all directories within the project directory. */ + final File directories[] = new File(PROJECT_DIR).listFiles(); + if (directories != null) { + for (File directory : directories) { + if (directory.isDirectory()) { + /* If there is a project file within the directory, we know the name of it. */ + final File projectFile = new File(directory, directory.getName() + FileManager.EXTENSION); + if (projectFile.exists()) { + try { + /* Try to load the project. */ + MIProject project = AnalysisController.loadFromFile(projectFile); + if (project != null) { + resultList.add(project); + } + } catch (Exception ex) { + FileManager.LOG.error("Could not load project '" + directory.getName() + "'."); + } + } + } + } + } + + return resultList; + } + + public static FileManager getInstance() { + return instance; + } +}