Java tutorial
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited * * 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 org.tibetjungle.demo.service; import java.util.List; import java.util.Random; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.support.ApplicationObjectSupport; import org.springframework.security.acls.domain.BasePermission; import org.springframework.security.acls.domain.PrincipalSid; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.tibetjungle.demo.dao.ContactDao; import org.tibetjungle.demo.model.Contact; import org.tibetjungle.security.service.AclPermissionService; /** * Concrete implementation of {@link ContactService}. * * @author Ben Alex */ @Transactional public class ContactServiceImpl extends ApplicationObjectSupport implements ContactService, InitializingBean { // ~ Instance fields // ================================================================================================ private ContactDao contactDao; @Autowired private AclPermissionService aclPermissionService = null; private int counter = 1000; // ~ Methods // ======================================================================================================== public void afterPropertiesSet() throws Exception { Assert.notNull(contactDao, "contactDao required"); Assert.notNull(aclPermissionService, "aclPermissionService required"); } public void create(Contact contact) { // Create the Contact itself contact.setId(new Long(counter++)); contactDao.create(contact); // Grant the current principal administrative permission to the contact aclPermissionService.grantAfterCreating(contact, new PrincipalSid(getUsername()), BasePermission.ADMINISTRATION); if (logger.isDebugEnabled()) { logger.debug( "Created contact " + contact + " and granted admin permission to recipient " + getUsername()); } } public void delete(Contact contact) { contactDao.delete(contact.getId()); // Delete the ACL information as well aclPermissionService.deleteAcl(contact); if (logger.isDebugEnabled()) { logger.debug("Deleted contact " + contact + " including ACL permissions"); } } @Transactional(readOnly = true) public List<Contact> getAll() { logger.debug("Returning all contacts"); return contactDao.findAll(); } @Transactional(readOnly = true) public List<String> getAllRecipients() { logger.debug("Returning all recipients"); return contactDao.findAllPrincipals(); } @Transactional(readOnly = true) public Contact getById(Long id) { if (logger.isDebugEnabled()) { logger.debug("Returning contact with id: " + id); } return contactDao.getById(id); } /** * This is a public method. */ @Transactional(readOnly = true) public Contact getRandomContact() { logger.debug("Returning random contact"); Random rnd = new Random(); List<Contact> contacts = contactDao.findAll(); int getNumber = rnd.nextInt(contacts.size()); return contacts.get(getNumber); } protected String getUsername() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth.getPrincipal() instanceof UserDetails) { return ((UserDetails) auth.getPrincipal()).getUsername(); } else { return auth.getPrincipal().toString(); } } public void setContactDao(ContactDao contactDao) { this.contactDao = contactDao; } public void update(Contact contact) { contactDao.update(contact); logger.debug("Updated contact " + contact); } }