Java Code Examples for javax.jdo.PersistenceManager#detachCopy()

The following examples show how to use javax.jdo.PersistenceManager#detachCopy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PrivilegeOperatePersistence.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public void revokePrivilege(PrivilegeObject privilege,MSentryRole role, PersistenceManager pm) throws SentryUserException {
  MSentryGMPrivilege mPrivilege = getPrivilege(convertToPrivilege(privilege), pm);
  if (mPrivilege == null) {
    mPrivilege = convertToPrivilege(privilege);
  } else {
    mPrivilege = (MSentryGMPrivilege) pm.detachCopy(mPrivilege);
  }

  Set<MSentryGMPrivilege> privilegeGraph = Sets.newHashSet();
  privilegeGraph.addAll(populateIncludePrivileges(Sets.newHashSet(role), mPrivilege, pm));

  /**
   * Get the privilege graph
   * populateIncludePrivileges will get the privileges that needed revoke
   */
  for (MSentryGMPrivilege persistedPriv : privilegeGraph) {
    /**
     * force to load all roles related this privilege
     * avoid the lazy-loading risk,such as:
     * if the roles field of privilege aren't loaded, then the roles is a empty set
     * privilege.removeRole(role) and pm.makePersistent(privilege)
     * will remove other roles that shouldn't been removed
     */
    revokeRolePartial(mPrivilege, persistedPriv, role, pm);
  }
  pm.makePersistent(role);
}
 
Example 2
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void alterSentryRoleRevokePrivilegeCore(PersistenceManager pm,
    String roleName, TSentryPrivilege tPrivilege)
    throws SentryNoSuchObjectException, SentryInvalidInputException {
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole mRole = (MSentryRole) query.execute(roleName);
  if (mRole == null) {
    throw new SentryNoSuchObjectException("Role: " + roleName + " doesn't exist");
  } else {
    query = pm.newQuery(MSentryPrivilege.class);
    MSentryPrivilege mPrivilege = getMSentryPrivilege(tPrivilege, pm);
    if (mPrivilege == null) {
      mPrivilege = convertToMSentryPrivilege(tPrivilege);
    } else {
      mPrivilege = (MSentryPrivilege) pm.detachCopy(mPrivilege);
    }

    Set<MSentryPrivilege> privilegeGraph = Sets.newHashSet();
    if (mPrivilege.getGrantOption() != null) {
      privilegeGraph.add(mPrivilege);
    } else {
      MSentryPrivilege mTure = new MSentryPrivilege(mPrivilege);
      mTure.setGrantOption(true);
      privilegeGraph.add(mTure);
      MSentryPrivilege mFalse = new MSentryPrivilege(mPrivilege);
      mFalse.setGrantOption(false);
      privilegeGraph.add(mFalse);
    }
    // Get the privilege graph
    populateChildren(pm, Sets.newHashSet(roleName), mPrivilege, privilegeGraph);
    for (MSentryPrivilege childPriv : privilegeGraph) {
      revokePrivilegeFromRole(pm, tPrivilege, mRole, childPriv);
    }
    pm.makePersistent(mRole);
  }
}
 
Example 3
Source File: AndroidRpcImpl.java    From flickr-uploader with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public AppInstall ensureInstall(AndroidDevice androidDevice) {
	PersistenceManager pm = PMF.get().getPersistenceManager();
	try {
		AppInstall appInstall;
		Query query = pm.newQuery(AppInstall.class);
		query.setFilter("deviceId == :param");
		List<AppInstall> result = (List<AppInstall>) query.execute(androidDevice.getId());
		if (result.isEmpty()) {
			logger.debug("New install : " + androidDevice);
			String email = androidDevice.getEmails().isEmpty() ? null : androidDevice.getEmails().iterator().next();
			sendEmail(STR.supportEmail, "[FlickrUploader] New install - " + androidDevice.getCountryCode() + " - " + androidDevice.getLanguage() + " - " + androidDevice.getAppVersion() + " - "
					+ email, androidDevice.getEmails() + " - " + androidDevice.getAndroidVersion() + " - " + androidDevice.getAppVersion(), STR.supportEmail);
			appInstall = new AppInstall(androidDevice.getId(), androidDevice, androidDevice.getEmails());
			pm.makePersistent(appInstall);

		} else {
			logger.debug("Updating install : " + androidDevice);
			appInstall = result.get(0);
			appInstall.setAndroidDevice(androidDevice);
		}
		return pm.detachCopy(appInstall);
	} catch (Exception e) {
		logger.error(ToolString.stack2string(e));
	} finally {
		pm.close();
	}
	return null;
}