|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | + |
| 20 | +package com.cloud.storage.template; |
| 21 | + |
| 22 | +import com.cloud.storage.Storage.ImageFormat; |
| 23 | +import com.cloud.storage.StorageLayer; |
| 24 | +import com.cloud.utils.component.AdapterBase; |
| 25 | +import org.apache.log4j.Logger; |
| 26 | + |
| 27 | +import javax.ejb.Local; |
| 28 | +import javax.naming.ConfigurationException; |
| 29 | +import java.io.File; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +@Local(value = Processor.class) |
| 33 | +public class TARProcessor extends AdapterBase implements Processor { |
| 34 | + private static final Logger s_logger = Logger.getLogger(TARProcessor.class); |
| 35 | + |
| 36 | + private StorageLayer _storage; |
| 37 | + |
| 38 | + @Override |
| 39 | + public FormatInfo process(String templatePath, ImageFormat format, String templateName) { |
| 40 | + if (format != null) { |
| 41 | + s_logger.debug("We currently don't handle conversion from " + format + " to TAR."); |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + String tarPath = templatePath + File.separator + templateName + "." + ImageFormat.TAR.getFileExtension(); |
| 46 | + |
| 47 | + if (!_storage.exists(tarPath)) { |
| 48 | + s_logger.debug("Unable to find the tar file: " + tarPath); |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + FormatInfo info = new FormatInfo(); |
| 53 | + info.format = ImageFormat.TAR; |
| 54 | + info.filename = templateName + "." + ImageFormat.TAR.getFileExtension(); |
| 55 | + |
| 56 | + File tarFile = _storage.getFile(tarPath); |
| 57 | + |
| 58 | + info.size = _storage.getSize(tarPath); |
| 59 | + |
| 60 | + info.virtualSize = getVirtualSize(tarFile); |
| 61 | + |
| 62 | + return info; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public long getVirtualSize(File file) { |
| 67 | + return file.length(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean configure(String name, Map<String, Object> params) throws ConfigurationException { |
| 72 | + _storage = (StorageLayer)params.get(StorageLayer.InstanceConfigKey); |
| 73 | + if (_storage == null) { |
| 74 | + throw new ConfigurationException("Unable to get storage implementation"); |
| 75 | + } |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | +} |
0 commit comments