use of javax.naming.InvalidNameException in project Towny by ElgarL.
the class TownyDatabaseHandler method renameTown.
@Override
public void renameTown(Town town, String newName) throws AlreadyRegisteredException, NotRegisteredException {
lock.lock();
String oldName;
try {
String filteredName;
try {
filteredName = NameValidation.checkAndFilterName(newName);
} catch (InvalidNameException e) {
throw new NotRegisteredException(e.getMessage());
}
if (hasTown(filteredName))
throw new AlreadyRegisteredException("The town " + filteredName + " is already in use.");
// TODO: Delete/rename any invites.
List<Resident> toSave = new ArrayList<Resident>(town.getResidents());
Boolean isCapital = false;
Nation nation = null;
Double townBalance = 0.0;
oldName = town.getName();
// Clear accounts
if (TownySettings.isUsingEconomy())
try {
townBalance = town.getHoldingBalance();
town.removeAccount();
} catch (EconomyException e) {
}
// Store the nation in case we have to update the capitol
if (town.hasNation()) {
nation = town.getNation();
isCapital = town.isCapital();
}
/*
* Tidy up old files.
* Has to be done here else the town no longer exists
* and the file move command may fail.
*/
deleteTown(town);
/*
* Remove the old town from the townsMap
* and rename to the new name
*/
universe.getTownsMap().remove(town.getName().toLowerCase());
town.setName(filteredName);
universe.getTownsMap().put(filteredName.toLowerCase(), town);
// If this was a nation capitol
if (isCapital) {
nation.setCapital(town);
}
if (TownySettings.isUsingEconomy()) {
//TODO
try {
town.setBalance(townBalance, "Rename Town - Transfer to new account");
} catch (EconomyException e) {
e.printStackTrace();
}
}
for (Resident resident : toSave) {
saveResident(resident);
}
for (TownBlock townBlock : town.getTownBlocks()) {
//townBlock.setTown(town);
saveTownBlock(townBlock);
}
saveTown(town);
saveTownList();
saveWorld(town.getWorld());
if (nation != null) {
saveNation(nation);
}
} finally {
lock.unlock();
}
BukkitTools.getPluginManager().callEvent(new RenameTownEvent(oldName, town));
universe.setChangedNotify(RENAME_TOWN);
}
use of javax.naming.InvalidNameException in project Towny by ElgarL.
the class TownyDatabaseHandler method renamePlayer.
@Override
public void renamePlayer(Resident resident, String newName) throws AlreadyRegisteredException, NotRegisteredException {
lock.lock();
String oldName = resident.getName();
try {
String filteredName;
try {
filteredName = NameValidation.checkAndFilterName(newName);
} catch (InvalidNameException e) {
throw new NotRegisteredException(e.getMessage());
}
//data needed for a new resident
double balance = 0.0D;
Town town = null;
long registered = 0L;
long lastOnline = 0L;
boolean isMayor = false;
//get data needed for resident
if (TownySettings.isUsingEconomy()) {
try {
balance = resident.getHoldingBalance();
resident.removeAccount();
} catch (EconomyException e) {
}
}
List<Resident> friends = resident.getFriends();
List<String> nationRanks = resident.getNationRanks();
TownyPermission permissions = resident.getPermissions();
String surname = resident.getSurname();
String title = resident.getTitle();
if (resident.hasTown()) {
town = resident.getTown();
}
List<TownBlock> townBlocks = resident.getTownBlocks();
List<String> townRanks = resident.getTownRanks();
registered = resident.getRegistered();
lastOnline = resident.getLastOnline();
isMayor = resident.isMayor();
//delete the resident and tidy up files
deleteResident(resident);
//remove old resident from residentsMap
//rename the resident
universe.getResidentMap().remove(oldName.toLowerCase());
resident.setName(filteredName);
universe.getResidentMap().put(filteredName.toLowerCase(), resident);
//add everything back to the resident
if (TownySettings.isUsingEconomy()) {
//TODO
try {
resident.setBalance(balance, "Rename Player - Transfer to new account");
} catch (EconomyException e) {
e.printStackTrace();
}
}
resident.setFriends(friends);
resident.setNationRanks(nationRanks);
//not sure if this will work
resident.setPermissions(permissions.toString());
resident.setSurname(surname);
resident.setTitle(title);
resident.setTown(town);
resident.setTownblocks(townBlocks);
resident.setTownRanks(townRanks);
resident.setRegistered(registered);
resident.setLastOnline(lastOnline);
if (isMayor) {
try {
town.setMayor(resident);
} catch (TownyException e) {
}
}
//save stuff
saveResidentList();
saveResident(resident);
if (town != null) {
saveTown(town);
}
for (TownBlock tb : townBlocks) {
saveTownBlock(tb);
}
//search and update all friends lists
Resident oldResident = new Resident(oldName);
List<Resident> toSaveResident = new ArrayList<Resident>(getResidents());
for (Resident toCheck : toSaveResident) {
if (toCheck.hasFriend(oldResident)) {
try {
toCheck.removeFriend(oldResident);
toCheck.addFriend(resident);
} catch (NotRegisteredException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for (Resident toCheck : toSaveResident) saveResident(toCheck);
} finally {
lock.unlock();
}
BukkitTools.getPluginManager().callEvent(new RenameResidentEvent(oldName, resident));
universe.setChangedNotify(RENAME_RESIDENT);
}
use of javax.naming.InvalidNameException in project Towny by ElgarL.
the class TownyFlatFileSource method saveResidentList.
@Override
public boolean saveResidentList() {
List<String> list = new ArrayList<String>();
for (Resident resident : getResidents()) {
try {
list.add(NameValidation.checkAndFilterPlayerName(resident.getName()));
} catch (InvalidNameException e) {
TownyMessaging.sendErrorMsg("Saving Error: Exception while saving town list file:" + resident.getName());
}
}
/*
* Make sure we only save in async
*/
this.queryQueue.add(new FlatFile_Task(list, rootFolder + dataFolder + FileMgmt.fileSeparator() + "residents.txt"));
return true;
}
use of javax.naming.InvalidNameException in project wildfly by wildfly.
the class ServiceBasedNamingStore method suffix.
private Name suffix(ServiceName parent, ServiceName child) {
String[] p = parent.toArray();
String[] c = child.toArray();
CompositeName name = new CompositeName();
for (int i = p.length; i < c.length; i++) {
try {
name.add(c[i]);
} catch (InvalidNameException e) {
throw new IllegalStateException(e);
}
}
return name;
}
use of javax.naming.InvalidNameException in project wildfly by wildfly.
the class InMemoryNamingStoreTestCase method testBindEmptyName.
@Test
public void testBindEmptyName() throws Exception {
try {
nameStore.bind(new CompositeName(), new Object(), Object.class);
fail("Should have thrown and InvalidNameException");
} catch (InvalidNameException expected) {
}
try {
nameStore.bind(new CompositeName(""), new Object(), Object.class);
fail("Should have thrown and InvalidNameException");
} catch (InvalidNameException expected) {
}
}
Aggregations