提交 e23599a0 编写于 作者: C CloudBees DEV@Cloud

Merge REMERGE_HEAD into HEAD

......@@ -459,6 +459,7 @@ public class User extends AbstractModelObject implements AccessControlled, Descr
* Gets all the users.
*/
public static Collection<User> getAll() {
final IdStrategy strategy = idStrategy();
if(System.currentTimeMillis() -lastScanned>10000) {
// occasionally scan the file system to check new users
// whether we should do this only once at start up or not is debatable.
......@@ -471,7 +472,7 @@ public class User extends AbstractModelObject implements AccessControlled, Descr
for (File subdir : subdirs)
if(new File(subdir,"config.xml").exists()) {
String name = subdir.getName();
String name = strategy.idFromFilename(subdir.getName());
User.getOrCreate(name, name, true);
}
......@@ -486,7 +487,6 @@ public class User extends AbstractModelObject implements AccessControlled, Descr
byNameLock.readLock().unlock();
}
Collections.sort(r,new Comparator<User>() {
IdStrategy strategy = idStrategy();
public int compare(User o1, User o2) {
return strategy.compare(o1.getId(), o2.getId());
......
......@@ -57,6 +57,16 @@ public abstract class IdStrategy extends AbstractDescribableImpl<IdStrategy> imp
@Nonnull
public abstract String filenameOf(@Nonnull String id);
/**
* Converts a filename into the corresponding id.
* @param filename the filename.
* @return the corresponding id.
* @since 1.577
*/
public String idFromFilename(@Nonnull String filename) {
return filename;
}
/**
* Converts an ID into a key for use in a Java Map.
*
......@@ -214,6 +224,59 @@ public abstract class IdStrategy extends AbstractDescribableImpl<IdStrategy> imp
}
}
@Override
public String idFromFilename(@Nonnull String filename) {
if (filename.matches("[a-z0-9_. -]+")) {
return filename;
} else {
StringBuilder buf = new StringBuilder(filename.length());
final char[] chars = filename.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ('a' <= c && c <= 'z') {
buf.append(c);
} else if ('0' <= c && c <= '9') {
buf.append(c);
} else if ('_' == c || '.' == c || '-' == c || ' ' == c || '@' == c) {
buf.append(c);
} else if (c == '~') {
i++;
if (i < chars.length) {
buf.append(Character.toUpperCase(chars[i]));
}
} else if (c == '$') {
StringBuilder hex = new StringBuilder(4);
i++;
if (i < chars.length) {
hex.append(chars[i]);
} else {
break;
}
i++;
if (i < chars.length) {
hex.append(chars[i]);
} else {
break;
}
i++;
if (i < chars.length) {
hex.append(chars[i]);
} else {
break;
}
i++;
if (i < chars.length) {
hex.append(chars[i]);
} else {
break;
}
buf.append(Character.valueOf((char)Integer.parseInt(hex.toString(), 16)));
}
}
return buf.toString();
}
}
/**
* {@inheritDoc}
*/
......
......@@ -174,6 +174,8 @@ public class UserTest {
User user = User.get("john smith");
User user2 = User.get("John Smith");
assertSame("Users should have the same id.", user.getId(), user2.getId());
assertEquals(user.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user.getId())));
assertEquals(user2.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user2.getId())));
}
@Test
......@@ -191,6 +193,8 @@ public class UserTest {
assertEquals("john smith", User.idStrategy().filenameOf(user.getId()));
assertEquals("John Smith", User.idStrategy().keyFor(user2.getId()));
assertEquals("~john ~smith", User.idStrategy().filenameOf(user2.getId()));
assertEquals(user.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user.getId())));
assertEquals(user2.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user2.getId())));
}
@Test
......@@ -212,6 +216,8 @@ public class UserTest {
assertEquals("Users should have the same id.", user.getId(), user2.getId());
assertEquals("john.smith@acme.org", User.idStrategy().keyFor(user2.getId()));
assertEquals("john.smith@acme.org", User.idStrategy().filenameOf(user2.getId()));
assertEquals(user.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user.getId())));
assertEquals(user2.getId(), User.idStrategy().idFromFilename(User.idStrategy().filenameOf(user2.getId())));
}
@Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册