mirror of https://github.com/apache/cloudstack.git
Merge branch 'master' into ui-restyle
This commit is contained in:
commit
d97be4f6eb
|
|
@ -25,8 +25,8 @@ import javax.ejb.Local;
|
|||
import javax.naming.ConfigurationException;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import org.apache.cloudstack.framework.config.impl.ConfigurationVO;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.utils.component.ComponentLifecycle;
|
||||
import com.cloud.utils.crypt.DBEncryptionUtil;
|
||||
|
|
@ -37,6 +37,7 @@ import com.cloud.utils.db.SearchCriteria;
|
|||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
|
||||
@Component
|
||||
@Local(value = {ConfigurationDao.class})
|
||||
public class ConfigurationDaoImpl extends GenericDaoBase<ConfigurationVO, String> implements ConfigurationDao {
|
||||
private static final Logger s_logger = Logger.getLogger(ConfigurationDaoImpl.class);
|
||||
|
|
|
|||
|
|
@ -16,35 +16,20 @@
|
|||
// under the License.
|
||||
package com.cloud.metadata;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.ejb.Local;
|
||||
import javax.inject.Inject;
|
||||
import javax.naming.ConfigurationException;
|
||||
|
||||
import com.cloud.server.ResourceMetaDataService;
|
||||
import com.cloud.storage.VolumeDetailVO;
|
||||
import com.cloud.storage.dao.VolumeDetailsDao;
|
||||
import com.cloud.vm.NicDetailVO;
|
||||
import com.cloud.vm.dao.NicDao;
|
||||
import com.cloud.vm.dao.NicDetailDao;
|
||||
|
||||
import org.apache.cloudstack.api.command.user.tag.ListTagsCmd;
|
||||
import org.apache.cloudstack.context.CallContext;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.cloud.api.query.dao.ResourceTagJoinDao;
|
||||
import com.cloud.api.query.vo.ResourceTagJoinVO;
|
||||
import com.cloud.domain.Domain;
|
||||
import com.cloud.event.ActionEvent;
|
||||
import com.cloud.event.EventTypes;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.network.dao.FirewallRulesDao;
|
||||
import com.cloud.network.dao.IPAddressDao;
|
||||
import com.cloud.network.dao.LoadBalancerDao;
|
||||
|
|
@ -54,32 +39,30 @@ import com.cloud.network.rules.dao.PortForwardingRulesDao;
|
|||
import com.cloud.network.security.dao.SecurityGroupDao;
|
||||
import com.cloud.network.vpc.dao.StaticRouteDao;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import com.cloud.projects.Project.ListProjectResourcesCriteria;
|
||||
import com.cloud.projects.dao.ProjectDao;
|
||||
import com.cloud.server.ResourceMetaDataService;
|
||||
import com.cloud.server.ResourceTag;
|
||||
import com.cloud.server.ResourceTag.TaggedResourceType;
|
||||
import com.cloud.server.TaggedResourceService;
|
||||
import com.cloud.storage.VolumeDetailVO;
|
||||
import com.cloud.storage.dao.SnapshotDao;
|
||||
import com.cloud.storage.dao.VMTemplateDao;
|
||||
import com.cloud.storage.dao.VolumeDao;
|
||||
import com.cloud.storage.dao.VolumeDetailsDao;
|
||||
import com.cloud.tags.dao.ResourceTagDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountManager;
|
||||
import com.cloud.user.DomainManager;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.Ternary;
|
||||
import com.cloud.utils.component.Manager;
|
||||
import com.cloud.utils.component.ManagerBase;
|
||||
import com.cloud.utils.db.DB;
|
||||
import com.cloud.utils.db.DbUtil;
|
||||
import com.cloud.utils.db.Filter;
|
||||
import com.cloud.utils.db.GenericDao;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
import com.cloud.utils.db.Transaction;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import com.cloud.uuididentity.dao.IdentityDao;
|
||||
import com.cloud.vm.NicDetailVO;
|
||||
import com.cloud.vm.UserVmDetailVO;
|
||||
import com.cloud.vm.dao.NicDao;
|
||||
import com.cloud.vm.dao.NicDetailDao;
|
||||
import com.cloud.vm.dao.UserVmDao;
|
||||
import com.cloud.vm.dao.UserVmDetailsDao;
|
||||
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
|
||||
|
||||
|
||||
|
|
@ -139,6 +122,8 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements Resource
|
|||
NicDao _nicDao;
|
||||
@Inject
|
||||
TaggedResourceService _taggedResourceMgr;
|
||||
@Inject
|
||||
UserVmDetailsDao _userVmDetail;
|
||||
|
||||
@Override
|
||||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
|
|
@ -215,9 +200,12 @@ public class ResourceMetaDataManagerImpl extends ManagerBase implements Resource
|
|||
if(resourceType == TaggedResourceType.Volume){
|
||||
VolumeDetailVO v = new VolumeDetailVO(id, key, value);
|
||||
_volumeDetailDao.persist(v);
|
||||
}else if (resourceType == TaggedResourceType.Nic){
|
||||
} else if (resourceType == TaggedResourceType.Nic){
|
||||
NicDetailVO n = new NicDetailVO(id, key, value);
|
||||
_nicDetailDao.persist(n);
|
||||
} else if (resourceType == TaggedResourceType.UserVm) {
|
||||
UserVmDetailVO v = new UserVmDetailVO(id, key, value);
|
||||
_userVmDetail.persist(v);
|
||||
}else{
|
||||
throw new InvalidParameterValueException("The resource type " + resourceType + " is not supported by the API yet");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.cloudstack.api.BaseCmd.HTTPMethod;
|
||||
import org.apache.cloudstack.framework.config.ConfigKey;
|
||||
|
||||
import com.cloud.agent.api.VmDiskStatsEntry;
|
||||
|
|
@ -110,4 +111,7 @@ public interface UserVmManager extends UserVmService {
|
|||
boolean setupVmForPvlan(boolean add, Long hostId, NicProfile nic);
|
||||
|
||||
void collectVmDiskStatistics (UserVmVO userVm);
|
||||
|
||||
UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha, Boolean isDisplayVmEnabled, Long osTypeId, String userData,
|
||||
Boolean isDynamicallyScalable, HTTPMethod httpMethod)throws ResourceUnavailableException, InsufficientCapacityException;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1768,43 +1768,29 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
|||
Boolean isDynamicallyScalable = cmd.isDynamicallyScalable();
|
||||
Account caller = CallContext.current().getCallingAccount();
|
||||
|
||||
// Input validation
|
||||
UserVmVO vmInstance = null;
|
||||
|
||||
// Verify input parameters
|
||||
vmInstance = _vmDao.findById(id.longValue());
|
||||
|
||||
// Input validation and permission checks
|
||||
UserVmVO vmInstance = _vmDao.findById(id.longValue());
|
||||
if (vmInstance == null) {
|
||||
throw new InvalidParameterValueException(
|
||||
"unable to find virtual machine with id " + id);
|
||||
}
|
||||
|
||||
ServiceOffering offering = _serviceOfferingDao.findById(vmInstance
|
||||
.getServiceOfferingId());
|
||||
if (!offering.getOfferHA() && ha != null && ha) {
|
||||
throw new InvalidParameterValueException(
|
||||
"Can't enable ha for the vm as it's created from the Service offering having HA disabled");
|
||||
}
|
||||
|
||||
_accountMgr.checkAccess(CallContext.current().getCallingAccount(), null, true,
|
||||
vmInstance);
|
||||
|
||||
if (displayName == null) {
|
||||
displayName = vmInstance.getDisplayName();
|
||||
}
|
||||
|
||||
if (ha == null) {
|
||||
ha = vmInstance.isHaEnabled();
|
||||
}
|
||||
|
||||
if (isDisplayVmEnabled == null) {
|
||||
isDisplayVmEnabled = vmInstance.isDisplayVm();
|
||||
} else{
|
||||
|
||||
if (isDisplayVmEnabled != null) {
|
||||
if(!_accountMgr.isRootAdmin(caller.getType())){
|
||||
throw new PermissionDeniedException( "Cannot update parameter displayvm, only admin permitted ");
|
||||
}
|
||||
}
|
||||
|
||||
return updateVirtualMachine(id, displayName, group, ha, isDisplayVmEnabled, osTypeId, userData, isDynamicallyScalable, cmd.getHttpMethod());
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserVm updateVirtualMachine(long id, String displayName, String group, Boolean ha,
|
||||
Boolean isDisplayVmEnabled, Long osTypeId, String userData, Boolean isDynamicallyScalable, HTTPMethod httpMethod)
|
||||
throws ResourceUnavailableException, InsufficientCapacityException {
|
||||
UserVmVO vm = _vmDao.findById(id);
|
||||
if (vm == null) {
|
||||
throw new CloudRuntimeException(
|
||||
|
|
@ -1816,45 +1802,50 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
|
|||
throw new InvalidParameterValueException("Vm with id " + id
|
||||
+ " is not in the right state");
|
||||
}
|
||||
|
||||
if (displayName == null) {
|
||||
displayName = vm.getDisplayName();
|
||||
}
|
||||
|
||||
if (ha == null) {
|
||||
ha = vm.isHaEnabled();
|
||||
}
|
||||
|
||||
ServiceOffering offering = _serviceOfferingDao.findById(vm.getServiceOfferingId());
|
||||
if (!offering.getOfferHA() && ha) {
|
||||
throw new InvalidParameterValueException(
|
||||
"Can't enable ha for the vm as it's created from the Service offering having HA disabled");
|
||||
}
|
||||
|
||||
if (isDisplayVmEnabled == null) {
|
||||
isDisplayVmEnabled = vm.isDisplayVm();
|
||||
}
|
||||
|
||||
boolean updateUserdata = false;
|
||||
if (userData != null) {
|
||||
// check and replace newlines
|
||||
userData = userData.replace("\\n", "");
|
||||
validateUserData(userData, cmd.getHttpMethod());
|
||||
validateUserData(userData, httpMethod);
|
||||
// update userData on domain router.
|
||||
updateUserdata = true;
|
||||
} else {
|
||||
userData = vmInstance.getUserData();
|
||||
}
|
||||
|
||||
String description = "";
|
||||
|
||||
if (displayName != null && !displayName.equals(vmInstance.getDisplayName())) {
|
||||
description += "New display name: " + displayName + ". ";
|
||||
}
|
||||
|
||||
if (ha != vmInstance.isHaEnabled()) {
|
||||
if (ha) {
|
||||
description += "Enabled HA. ";
|
||||
} else {
|
||||
description += "Disabled HA. ";
|
||||
}
|
||||
}
|
||||
if (osTypeId == null) {
|
||||
osTypeId = vmInstance.getGuestOSId();
|
||||
} else {
|
||||
description += "Changed Guest OS Type to " + osTypeId + ". ";
|
||||
}
|
||||
|
||||
if (group != null) {
|
||||
if (addInstanceToGroup(id, group)) {
|
||||
description += "Added to group: " + group + ".";
|
||||
}
|
||||
userData = vm.getUserData();
|
||||
}
|
||||
|
||||
if (isDynamicallyScalable == null) {
|
||||
isDynamicallyScalable = vmInstance.isDynamicallyScalable();
|
||||
isDynamicallyScalable = vm.isDynamicallyScalable();
|
||||
}
|
||||
|
||||
if (osTypeId == null) {
|
||||
osTypeId = vm.getGuestOSId();
|
||||
}
|
||||
|
||||
if (group != null) {
|
||||
addInstanceToGroup(id, group);
|
||||
}
|
||||
|
||||
if (isDynamicallyScalable == null) {
|
||||
isDynamicallyScalable = vm.isDynamicallyScalable();
|
||||
}
|
||||
|
||||
_vmDao.updateVM(id, displayName, ha, osTypeId, userData, isDisplayVmEnabled, isDynamicallyScalable);
|
||||
|
|
|
|||
|
|
@ -408,13 +408,12 @@ class TestAddMultipleNSDiffZone(cloudstackTestCase):
|
|||
for zone in zones:
|
||||
if zone.networktype == 'Advanced':
|
||||
zone_list.append(zone)
|
||||
|
||||
self.assertGreater(
|
||||
len(zone_list),
|
||||
1,
|
||||
"Atleast 2 advanced mode zones should be present for this test"
|
||||
)
|
||||
|
||||
zoneid=zone_list[0].id
|
||||
physical_networks = PhysicalNetwork.list(
|
||||
self.apiclient,
|
||||
zoneid=zone_list[0].id
|
||||
|
|
@ -424,43 +423,12 @@ class TestAddMultipleNSDiffZone(cloudstackTestCase):
|
|||
True,
|
||||
"There should be atleast one physical network for advanced zone"
|
||||
)
|
||||
physical_network = physical_networks[0]
|
||||
self.debug("Adding netscaler device: %s" %
|
||||
self.services["netscaler_1"]["ipaddress"])
|
||||
netscaler_1 = NetScaler.add(
|
||||
self.apiclient,
|
||||
self.services["netscaler_1"],
|
||||
physicalnetworkid=physical_network.id
|
||||
)
|
||||
netscaler_1 = add_netscaler(self.apiclient, zoneid, self.services["netscaler_1"])
|
||||
self.cleanup.append(netscaler_1)
|
||||
self.debug("Checking if Netscaler network service provider is enabled?")
|
||||
|
||||
nw_service_providers = NetworkServiceProvider.list(
|
||||
self.apiclient,
|
||||
name='Netscaler',
|
||||
physicalnetworkid=physical_network.id
|
||||
)
|
||||
self.assertEqual(
|
||||
isinstance(nw_service_providers, list),
|
||||
True,
|
||||
"Network service providers list should not be empty"
|
||||
)
|
||||
netscaler_provider = nw_service_providers[0]
|
||||
if netscaler_provider.state != 'Enabled':
|
||||
self.debug("Netscaler provider is not enabled. Enabling it..")
|
||||
response = NetworkServiceProvider.update(
|
||||
self.apiclient,
|
||||
id=netscaler_provider.id,
|
||||
state='Enabled'
|
||||
)
|
||||
self.assertEqual(
|
||||
response.state,
|
||||
"Enabled",
|
||||
"Network service provider should be in enabled state"
|
||||
)
|
||||
else:
|
||||
self.debug("Netscaler service provider is already enabled.")
|
||||
|
||||
physical_network = physical_networks[0]
|
||||
ns_list = NetScaler.list(
|
||||
self.apiclient,
|
||||
lbdeviceid=netscaler_1.lbdeviceid
|
||||
|
|
@ -492,6 +460,7 @@ class TestAddMultipleNSDiffZone(cloudstackTestCase):
|
|||
self.apiclient,
|
||||
zoneid=zone_list[1].id
|
||||
)
|
||||
zoneid=zone_list[1].id
|
||||
self.assertEqual(
|
||||
isinstance(physical_networks, list),
|
||||
True,
|
||||
|
|
@ -501,11 +470,7 @@ class TestAddMultipleNSDiffZone(cloudstackTestCase):
|
|||
|
||||
self.debug("Adding netscaler device: %s" %
|
||||
self.services["netscaler_2"]["ipaddress"])
|
||||
netscaler_2 = NetScaler.add(
|
||||
self.apiclient,
|
||||
self.services["netscaler_2"],
|
||||
physicalnetworkid=physical_network.id
|
||||
)
|
||||
netscaler_2 = add_netscaler(self.apiclient, zoneid, self.services["netscaler_2"])
|
||||
self.cleanup.append(netscaler_2)
|
||||
ns_list = NetScaler.list(
|
||||
self.apiclient,
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@
|
|||
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
|
||||
<context:annotation-config />
|
||||
<context:component-scan base-package="com.cloud.usage, com.cloud.event.dao, com.cloud.user.dao, com.cloud.configuration.dao, com.cloud.alert.dao, com.cloud.domain.dao">
|
||||
<context:component-scan base-package="com.cloud.usage, com.cloud.event.dao, com.cloud.user.dao, com.cloud.configuration.dao, com.cloud.alert.dao, com.cloud.domain.dao, org.apache.cloudstack.framework.config.dao">
|
||||
<context:exclude-filter type="assignable" expression="com.cloud.usage.UsageServiceImpl"/>
|
||||
</context:component-scan>
|
||||
|
||||
|
|
|
|||
|
|
@ -23,135 +23,21 @@ import java.util.Date;
|
|||
import java.util.Formatter;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
|
||||
public class NumbersUtil {
|
||||
public static long parseLong(String s, long defaultValue) {
|
||||
if (s == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
try {
|
||||
return Long.parseLong(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
return NumberUtils.toLong(s, defaultValue);
|
||||
}
|
||||
|
||||
public static int parseInt(String s, int defaultValue) {
|
||||
if (s == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
return NumberUtils.toInt(s, defaultValue);
|
||||
}
|
||||
|
||||
public static float parseFloat(String s, float defaultValue) {
|
||||
if (s == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Float.parseFloat(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts bytes to short on input.
|
||||
*/
|
||||
public static int bytesToShort(byte b[]) {
|
||||
return (b[1] & 0xff) | ((b[0] << 8) & 0xff00);
|
||||
}
|
||||
|
||||
public static int bytesToShort(byte b[], int pos) {
|
||||
return (b[pos + 1] & 0xff) | ((b[pos] << 8) & 0xff00);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts bytes to long on input.
|
||||
*/
|
||||
public static int bytesToInt(byte b[]) {
|
||||
return bytesToInt(b, 0);
|
||||
}
|
||||
|
||||
public static int bytesToInt(byte b[], int pos) {
|
||||
int value = b[pos + 3] & 0xff;
|
||||
value |= (b[pos + 2] << 8) & 0xff00;
|
||||
value |= (b[pos + 1] << 16) & 0xff0000;
|
||||
value |= (b[pos] << 24) & 0xff000000;
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a short to a series of bytes for output. Must be 2 bytes long.
|
||||
*/
|
||||
public static byte[] shortToBytes(int n) {
|
||||
byte b[] = new byte[2];
|
||||
b[1] = (byte)(n & 0xff);
|
||||
b[0] = (byte)((n >> 8) & 0xff);
|
||||
return b;
|
||||
}
|
||||
|
||||
public static char encodeByte(int b) {
|
||||
if (b < 10) {
|
||||
return (char)(b + '0');
|
||||
} else if (b < 36) {
|
||||
return (char)(b - 10 + 'A');
|
||||
} else if (b < 62) {
|
||||
return (char)(b - 36 + 'a');
|
||||
} else if (b == 62) {
|
||||
return '(';
|
||||
} else if (b == 63) {
|
||||
return ')';
|
||||
}
|
||||
return (char)255;
|
||||
}
|
||||
|
||||
public static int decodeByte(char b) {
|
||||
if (b >= 'A' && b <= 'Z') {
|
||||
return b + 10 - 'A';
|
||||
} else if (b >= 'a' && b <= 'z') {
|
||||
return b + 36 - 'a';
|
||||
} else if (b >= '0' && b <= '9') {
|
||||
return b - '0';
|
||||
} else if (b == ')') {
|
||||
return 63;
|
||||
} else if (b == '(') {
|
||||
return 62;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a long to a series of bytes for output. Must be 4 bytes long.
|
||||
*/
|
||||
public static byte[] intToBytes(int n) {
|
||||
byte b[] = new byte[4];
|
||||
b[3] = (byte)(n & 0xff);
|
||||
b[2] = (byte)((n >> 8) & 0xff);
|
||||
b[1] = (byte)((n >> 16) & 0xff);
|
||||
b[0] = (byte)((n >> 24) & 0xff);
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorry for the bad naming but the longToBytes is already taken. Returns an 8 byte long byte array.
|
||||
**/
|
||||
public static byte[] longToBytes(long n) {
|
||||
byte b[] = new byte[8];
|
||||
b[7] = (byte)(n & 0xff);
|
||||
b[6] = (byte)((n >> 8) & 0xff);
|
||||
b[5] = (byte)((n >> 16) & 0xff);
|
||||
b[4] = (byte)((n >> 24) & 0xff);
|
||||
b[3] = (byte)((n >> 32) & 0xff);
|
||||
b[2] = (byte)((n >> 40) & 0xff);
|
||||
b[1] = (byte)((n >> 48) & 0xff);
|
||||
b[0] = (byte)((n >> 56) & 0xff);
|
||||
return b;
|
||||
return NumberUtils.toFloat(s, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -162,19 +48,7 @@ public class NumbersUtil {
|
|||
}
|
||||
|
||||
public static long bytesToLong(byte b[], int pos) {
|
||||
ByteBuffer buf = ByteBuffer.wrap(b, pos, 8);
|
||||
return buf.getLong();
|
||||
/*
|
||||
* long value = b[pos + 7] & 0xff;
|
||||
* value |= (b[pos + 6] << 8) & 0xff00;
|
||||
* value |= (b[pos + 5] << 16) & 0xff0000;
|
||||
* value |= (b[pos + 4] << 24) & 0xff000000;
|
||||
* value |= (b[pos + 3] << 32) & 0xff00000000;
|
||||
* value |= (b[pos + 2] << 40) & 0xff0000000000;
|
||||
* value |= (b[pos + 1] << 48) & 0xff000000000000;
|
||||
* value |= (b[pos + 0] << 56) & 0xff00000000000000;
|
||||
* return value;
|
||||
*/
|
||||
return ByteBuffer.wrap(b, pos, 8).getLong();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -198,28 +72,22 @@ public class NumbersUtil {
|
|||
protected static final long TB = 1024 * GB;
|
||||
|
||||
public static String toReadableSize(long bytes) {
|
||||
if (bytes <= KB && bytes >= 0) {
|
||||
if (bytes < KB && bytes >= 0) {
|
||||
return Long.toString(bytes) + " bytes";
|
||||
}
|
||||
StringBuilder builder = new StringBuilder();
|
||||
Formatter format = new Formatter(builder, Locale.getDefault());
|
||||
if (bytes < MB) {
|
||||
format.format("%.2f KB", (float)bytes / (float)KB);
|
||||
format.close();
|
||||
return builder.toString();
|
||||
} else if (bytes < GB) {
|
||||
format.format("%.2f MB", (float)bytes / (float)MB);
|
||||
format.close();
|
||||
return builder.toString();
|
||||
} else if (bytes < TB) {
|
||||
format.format("%.2f GB", (float)bytes / (float)GB);
|
||||
format.close();
|
||||
return builder.toString();
|
||||
} else {
|
||||
format.format("%.4f TB", (float)bytes / (float)TB);
|
||||
format.close();
|
||||
return builder.toString();
|
||||
}
|
||||
format.close();
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -261,9 +129,4 @@ public class NumbersUtil {
|
|||
public static int hash(long value) {
|
||||
return (int)(value ^ (value >>> 32));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
long interval = parseInterval(args[0], -1);
|
||||
System.out.println(args[0] + " is " + interval);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,9 +23,25 @@ import org.junit.Test;
|
|||
public class NumbersUtilTest {
|
||||
|
||||
@Test
|
||||
public void formattingCheck() {
|
||||
long size = 1024 * 1024 * 1024;
|
||||
String formatted = NumbersUtil.toReadableSize(size);
|
||||
assertEquals("1.00 GB", formatted);
|
||||
public void toReadableSize() {
|
||||
assertEquals("1.0000 TB",
|
||||
NumbersUtil.toReadableSize((1024l * 1024l * 1024l * 1024l)));
|
||||
assertEquals("1.00 GB",
|
||||
NumbersUtil.toReadableSize((long) (1024 * 1024 * 1024)));
|
||||
assertEquals("1.00 MB",
|
||||
NumbersUtil.toReadableSize((long) (1024 * 1024)));
|
||||
assertEquals("1.00 KB", NumbersUtil.toReadableSize((long) (1024)));
|
||||
assertEquals("1023 bytes", NumbersUtil.toReadableSize((long) (1023)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bytesToLong() {
|
||||
assertEquals(0,
|
||||
NumbersUtil.bytesToLong(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }));
|
||||
assertEquals(1,
|
||||
NumbersUtil.bytesToLong(new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }));
|
||||
assertEquals(257,
|
||||
NumbersUtil.bytesToLong(new byte[] { 0, 0, 0, 0, 0, 0, 1, 1 }));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue