alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/******************************************************************************* 
 * Copyright (c) 2000, 2005 IBM Corporation and others. 
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution, and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 * 
 * Contributors: 
 *     IBM Corporation - initial API and implementation 
 *     IBM Corporation - added the following constants
 *******************************************************************************/

package eclipseCon2005.KCH.AuthDemo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import org.eclipse.jface.util.ListenerList;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;

import eclipseCon2005.KCH.AuthDemo.perspectives.AuthPerspective;
import eclipseCon2005.KCH.AuthDemo.perspectives.NoAuthPerspective;

public class Auth {

	public interface AuthListener {
		public void authChanged();
	}
	
	private ListenerList listenerList = new ListenerList(); 
	
	private AuthEntry currentUser; 
	
	private class AuthEntry {
		String user;

		String password;

		Set activites;
	}

	private static Auth instance;

	private List authTable = new ArrayList();

	private Auth() {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					AuthDemoPlugin.getDefault().getBundle().getResource(
							"authtable").openStream()));

			for (String line = reader.readLine(); line != null; line = reader.readLine()) {
				AuthEntry entry = new AuthEntry();
				StringTokenizer tok = new StringTokenizer(line, " ");
				entry.user = tok.nextToken();
				entry.password = tok.nextToken();
				String activities = tok.nextToken();
				entry.activites = new HashSet(Arrays.asList(activities.split(",")));
				
				authTable.add(entry);
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Auth getInstance() {
		if (instance == null) {
			instance = new Auth();
		}

		return instance;
	}

	public boolean login(String user, String password) {
		for (Iterator i = authTable.iterator(); i.hasNext();) {
			AuthEntry entry = (AuthEntry) i.next();
			if (entry.user.equals(user) && entry.password.equals(password)) {
				IWorkbench workbench = PlatformUI.getWorkbench();
				workbench.getActivitySupport().setEnabledActivityIds(
						entry.activites);
				workbench.getActiveWorkbenchWindow().getActivePage()
						.setPerspective(
								workbench.getPerspectiveRegistry()
										.findPerspectiveWithId(
												AuthPerspective.ID));				
				currentUser = entry;
				notifyListeners();
				return true;
			}
		}

		return false;
	}

	private void notifyListeners() {
		Object [] listeners = listenerList.getListeners();
		for (int k = 0; k < listeners.length; k++) {
			AuthListener l = (AuthListener) listeners[k];
			l.authChanged();
		}
	}

	public void logout() {
		currentUser = null;
		IWorkbench workbench = PlatformUI.getWorkbench();
		workbench.getActivitySupport().setEnabledActivityIds(
				Collections.EMPTY_SET);
		workbench.getActiveWorkbenchWindow().getActivePage().closePerspective(workbench.getPerspectiveRegistry()
				.findPerspectiveWithId(
						AuthPerspective.ID), false, false);
		workbench.getActiveWorkbenchWindow().getActivePage().setPerspective(
				workbench.getPerspectiveRegistry().findPerspectiveWithId(
						NoAuthPerspective.ID));
		notifyListeners();
	}
	
	public String getUserName() {
		return currentUser == null ? null : currentUser.user;
	}
	
	public boolean isManager() {
		return currentUser == null ? false : currentUser.activites.contains("managerActivity");
	}

	public void addAuthListener(AuthListener listener) {
		listenerList.add(listener);
	}
	
	public void removeAuthListener(AuthListener listener) {
		listenerList.remove(listener);
	}

	public Object[] getUserData() {
		ArrayList users = new ArrayList();
		for (Iterator i = authTable.iterator(); i.hasNext();) {
			AuthEntry element = (AuthEntry) i.next();
			users.add(new Object[] {element.user, element.activites.contains("managerActivity") ? "boss" : "monkey"});
		}
		
		return users.toArray();
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.