mirror of
https://github.com/google/nomulus.git
synced 2025-05-01 12:37:52 +02:00
Runnable and Callable are both @FunctionalInterfaces. The difference is that Callable requires a return value whereas Runnable does not, so in situations where we don't care about a return value, rather than having to add an unnecessary 'return null;' at the end of the lambda, we can simply use a non-returning Runnable instead. Unfortunately, owing to legacy reasons, Runnable is not declared to throw checked exceptions whereas Callable is, so in situations where checked exceptions are thrown we still need to have a 'return null;' call at the end. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=172935400
64 lines
2.4 KiB
Java
64 lines
2.4 KiB
Java
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// 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 google.registry.model.common;
|
|
|
|
import static google.registry.model.ofy.ObjectifyService.allocateId;
|
|
import static google.registry.model.ofy.ObjectifyService.ofy;
|
|
|
|
import com.google.appengine.api.users.User;
|
|
import com.google.common.base.Splitter;
|
|
import com.googlecode.objectify.annotation.Entity;
|
|
import com.googlecode.objectify.annotation.Id;
|
|
import google.registry.model.ImmutableObject;
|
|
import google.registry.model.annotations.NotBackedUp;
|
|
import google.registry.model.annotations.NotBackedUp.Reason;
|
|
|
|
/**
|
|
* A helper class to convert email addresses to GAE user ids. It does so by persisting a User
|
|
* object with the email address to Datastore, and then immediately reading it back.
|
|
*/
|
|
@Entity
|
|
@NotBackedUp(reason = Reason.TRANSIENT)
|
|
public class GaeUserIdConverter extends ImmutableObject {
|
|
|
|
@Id
|
|
public long id;
|
|
|
|
User user;
|
|
|
|
/**
|
|
* Converts an email address to a GAE user id.
|
|
*
|
|
* @return Numeric GAE user id (in String form), or null if email address has no GAE id
|
|
*/
|
|
public static String convertEmailAddressToGaeUserId(String emailAddress) {
|
|
final GaeUserIdConverter gaeUserIdConverter = new GaeUserIdConverter();
|
|
gaeUserIdConverter.id = allocateId();
|
|
gaeUserIdConverter.user =
|
|
new User(emailAddress, Splitter.on('@').splitToList(emailAddress).get(1));
|
|
|
|
try {
|
|
// Perform these operations in a transactionless context to avoid enlisting in some outer
|
|
// transaction (if any).
|
|
ofy().doTransactionless(() -> ofy().saveWithoutBackup().entity(gaeUserIdConverter).now());
|
|
|
|
// The read must be done in its own transaction to avoid reading from the session cache.
|
|
return ofy()
|
|
.transactNew(() -> ofy().load().entity(gaeUserIdConverter).safe().user.getUserId());
|
|
} finally {
|
|
ofy().doTransactionless(() -> ofy().deleteWithoutBackup().entity(gaeUserIdConverter).now());
|
|
}
|
|
}
|
|
}
|