Java tutorial
/* * Copyright 2012 AMG.lab, a Bull Group Company * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.xlcloud.service.manager; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.Calendar; import javax.inject.Inject; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.log4j.Logger; import org.json.JSONException; import org.json.JSONObject; import org.xlcloud.config.ConfigParam; import org.xlcloud.service.exception.VcmsException; import org.xlcloud.service.exception.VcmsObjectNotFoundException; import org.xlcloud.service.heat.parsers.TemplateParser; import org.xlcloud.service.heat.template.Template; /** * Implementation of {@link VirtualClusterTemplateManager} * * @author Konrad Krl, AMG.net * @author Piotr Kulasek-Szwed, AMG.net */ public class VirtualClusterTemplateManagerImpl implements VirtualClusterTemplateManager { private static Logger LOG = Logger.getLogger(VirtualClusterTemplateManagerImpl.class); private static final int MAX_NUMBER_OF_TRIES = 5; private static final String TEMPLATE_FILE_EXT = ".template"; /** * Defined in vcms.properties. Indicates a directory path where virtual * cluster templates should be saved. */ @Inject @ConfigParam private String templateRepositoryPath; /** {@inheritDoc} */ @Override public Template get(String referencePath) throws VcmsObjectNotFoundException { try { JSONObject templateJson = new JSONObject(readFile(referencePath)); TemplateParser parser = new TemplateParser(); return parser.fromJSON(templateJson); } catch (FileNotFoundException e) { LOG.error("The requested file: " + referencePath + " could not be found.", e); throw new VcmsObjectNotFoundException("Could not find the virtual cluster template."); } catch (IOException e) { LOG.error("Could not read from the requested file: " + referencePath, e); throw new VcmsObjectNotFoundException("Could not find the virtual cluster template."); } catch (JSONException e) { LOG.error("Could not read json content from the requested file: " + referencePath, e); throw new VcmsObjectNotFoundException("Could not read json content for the virutal cluter template."); } } /** {@inheritDoc} */ @Override public String save(Template virtualClusterTemplate, String definitionName) throws VcmsException { try { File file = createFileWithUniqueName(definitionName + TEMPLATE_FILE_EXT); BufferedWriter output = new BufferedWriter(new FileWriter(file)); output.write(virtualClusterTemplate.getTemplateContent()); output.close(); return file.getAbsolutePath(); } catch (IOException e) { throw new VcmsException(500, "Could not save the template file: " + e); } catch (JSONException e) { throw new VcmsException(500, "Could not save the template file: " + e); } } /** {@inheritDoc} */ @Override public void update(String referencePath, Template virtualClusterTemplate) { File file = FileUtils.getFile(referencePath); try { FileUtils.writeStringToFile(file, virtualClusterTemplate.getTemplateContent()); } catch (IOException e) { throw new VcmsException(500, "Could not update the template file: " + e); } catch (JSONException e) { throw new VcmsException(500, "Could not update the template file: " + e); } } @Override public void remove(String referencePath) { File file = FileUtils.getFile(referencePath); try { FileUtils.forceDelete(file); } catch (IOException e) { throw new VcmsException(500, "Could not remove the template file: " + e); } } private static String readFile(String path) throws IOException, FileNotFoundException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } } private File createFileWithUniqueName(String preferredFileName) throws IOException { File file = new File(templateRepositoryPath + "/" + preferredFileName); boolean fileCreated = file.createNewFile(); if (fileCreated) { return file; } // in case that a file with preferred name already exist // add a timestamp to the file name Calendar calendar = Calendar.getInstance(); String baseName = FilenameUtils.getBaseName(preferredFileName); String extName = FilenameUtils.getExtension(preferredFileName); int tries = 0; while (!fileCreated && tries < MAX_NUMBER_OF_TRIES) { tries += 1; file = new File(templateRepositoryPath + "/" + baseName + calendar.getTimeInMillis() + "." + extName); fileCreated = file.createNewFile(); } if (!fileCreated) { throw new IOException( "Could not save file: " + preferredFileName + ". Maximum number of tries exceeded."); } return file; } }