001: /**
002: * $Id: DPTreeHelper.java,v 1.7 2005/10/11 03:03:38 ru111118 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.desktop.admin.mbeans.tasks;
014:
015: import com.sun.portal.desktop.dp.DPNode;
016: import com.sun.portal.desktop.dp.DPChannel;
017: import com.sun.portal.desktop.dp.DPContainerChannel;
018: import com.sun.portal.admin.common.DesktopConstants;
019: import com.sun.portal.taskadmin.ChannelTaskAdmin;
020: import com.sun.portal.taskadmin.TaskAdminException;
021: import com.iplanet.sso.SSOToken;
022:
023: import java.util.logging.Logger;
024: import java.util.logging.Level;
025: import java.util.Collections;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Set;
030: import java.util.TreeSet;
031:
032: public class DPTreeHelper extends ChannelTaskAdmin implements
033: DesktopConstants {
034:
035: private Logger logger = null;
036: // This list is maintained to ensure that desktop container
037: // recursion does not occur due to the customer unknowingly
038: private List hierarchy = new ArrayList();
039:
040: public void setLogger(Logger pLogger) {
041: logger = pLogger;
042: }
043:
044: /**
045: * Constructor Creates a new instance of DPTreeHelper
046: */
047: public DPTreeHelper(SSOToken token, String baseDN, String portalId)
048: throws TaskAdminException {
049: super (token, baseDN, portalId);
050: }
051:
052: public Object[] getVisualTree(String rootContainer)
053: throws TaskAdminException {
054:
055: return processNode(rootContainer, TYPE_ROOT_CONTAINER_NODE);
056: }
057:
058: private Object[] processNode(String channelName, String channelType)
059: throws TaskAdminException {
060:
061: // The processNode is called whenever a new branch is being processed
062: // So when entering this branch of the tree add the currentNode i.e.
063: // the branch name to the list parents in the hierarchy for the current
064: // branch node
065: hierarchy.add(channelName);
066:
067: logger.log(Level.FINEST, "Processing Channel : " + channelName);
068: // Define a List for the channels available at the current node
069: // The list will be null if the current node is a leaf channel
070: Set chNames = Collections.EMPTY_SET;
071: List children = null;
072: // If type of currentNode is < 3 then its of type container
073: // Only then try to get the list of available channels
074: if (Integer.parseInt(channelType) < 3) {
075: // Get an unsorted list of available channels
076: List chNamesList = getAvailableChannels(channelName);
077: // If the list is not null. Sort them by putting into a TreeSet
078: if (chNamesList != null) {
079: chNames = new TreeSet(chNamesList);
080: }
081: }
082: logger.log(Level.FINEST, "Set of Children : " + chNames);
083:
084: if (!chNames.isEmpty()) {
085: children = new ArrayList(chNames.size());
086: }
087: // Iterate the children to prepare a list of object arrays representing
088: // the child nodes of the current channel
089: Iterator iterator = chNames.iterator();
090: while (iterator.hasNext()) {
091: // Define an Object array representing this child
092: Object[] child = null;
093: String childChannelName = (String) iterator.next();
094: // Check of the children of this node contain any container thats
095: // already in the hierarchy of the current node being processed
096: if (hierarchy.contains(childChannelName)) {
097: // If it exists then this will cause desktop recursion.
098: // Throw exception.
099: throw new TaskAdminException(
100: "Recursion Detected in Desktop");
101: // TODO - Pass the right error code or handle this situation
102: // gracefully if possible
103: }
104:
105: logger.log(Level.FINEST, "Child Channel Name : "
106: + childChannelName);
107: // Check if channel is displayable
108: if (!isDisplayable(childChannelName)) {
109: logger.log(Level.FINEST, "Child Not Displayable: "
110: + childChannelName);
111: continue;
112: }
113: // If all channels requested returns everything. Else evaluate the
114: // ones defined at this baseDN.
115: if (!canAccess(childChannelName)) {
116: logger.log(Level.FINEST, "Child Not Accessible: "
117: + childChannelName);
118: continue;
119: }
120: // Check if this channel is a container and if it is get
121: // the list of channels defined in the container
122: DPChannel childChannel = getDPChannel(childChannelName);
123: List selectedChannels = getSelectedChannels(channelName);
124: if (childChannel instanceof DPContainerChannel) {
125: if (selectedChannels.contains(childChannelName)) {
126: child = processNode(childChannelName,
127: TYPE_VISIBLE_CONTAINER_NODE);
128: logger.log(Level.FINEST, "Channel Type : "
129: + TYPE_VISIBLE_CONTAINER_NODE);
130: } else {
131: child = processNode(childChannelName,
132: TYPE_INVISIBLE_CONTAINER_NODE);
133: logger.log(Level.FINEST, "Channel Type : "
134: + TYPE_INVISIBLE_CONTAINER_NODE);
135: }
136: } else {
137: String type;
138: if (selectedChannels.contains(childChannelName)) {
139: type = getChannelType(childChannelName, true);
140: child = processNode(childChannelName, type);
141: } else {
142: type = getChannelType(childChannelName, false);
143: child = processNode(childChannelName, type);
144: }
145: logger.log(Level.FINEST, "Channel Type : " + type);
146: }
147: children.add(child);
148: }
149:
150: // Remove the current node from the list of parents before proceeding
151: // to the next branch of the tree
152: hierarchy.remove(channelName);
153:
154: Object[] currNode = { channelName, channelType, children };
155: return currNode;
156: }
157:
158: private String getChannelType(String childChannelName,
159: boolean isSelected) {
160: String classname = null;
161: String type = TYPE_VISIBLE_CHANNEL_NODE;
162: try {
163: classname = getClassName(childChannelName);
164: if (classname != null) {
165: if (classname.endsWith(PORTLET_CLASS)) {
166: if (isSelected) {
167: type = TYPE_VISIBLE_PORTLET_CHANNEL_NODE;
168: } else {
169: type = TYPE_INVISIBLE_PORTLET_CHANNEL_NODE;
170: }
171: } else if (classname.endsWith(REMOTE_PORTLET_CLASS)) {
172: if (isSelected) {
173: type = TYPE_VISIBLE_REMOTE_PORTLET_CHANNEL_NODE;
174: } else {
175: type = TYPE_INVISIBLE_REMOTE_PORTLET_CHANNEL_NODE;
176: }
177: } else {
178: if (isSelected) {
179: type = TYPE_VISIBLE_CHANNEL_NODE;
180: } else {
181: type = TYPE_INVISIBLE_CHANNEL_NODE;
182: }
183: }
184: }
185: } catch (TaskAdminException tae) {
186: //nothing, assume its a visible provider channel
187: }
188: return type;
189: }
190:
191: public Object[] getPhysicalTree() throws TaskAdminException {
192: Object[] topNode = processDPNode(null, TYPE_ROOT_CONTAINER_NODE);
193: String topNodeName = dpRoot.getName();
194: // If there is no DP at the current DN the DP API is returning empty
195: // string. Working around this issue by hardcoding it to _root in that
196: // situation.
197: if (topNodeName.length() < 1) {
198: topNodeName = "_root";
199: }
200: topNode[0] = topNodeName;
201: return topNode;
202: }
203:
204: private Object[] processDPNode(String channelName,
205: String channelType) throws TaskAdminException {
206:
207: logger.log(Level.FINEST, "Processing Channel : " + channelName);
208:
209: // Set current DPNode to null
210: DPNode currDPNode = null;
211: if (channelName != null) {
212: currDPNode = getDPChannel(channelName);
213: } else {
214: // This is the first iteration set it to DPRoot
215: currDPNode = dpRoot;
216: }
217:
218: // Define a List for the channels available at the current node
219: // The list will be null if the current node is a leaf channel
220: Set chNames = Collections.EMPTY_SET;
221: List children = null;
222: // If the channelType is a container
223: if (Integer.parseInt(channelType) < 3) {
224: // Sorting the element based on natural order
225: chNames = new TreeSet(getChannelNames(currDPNode));
226: }
227: logger.log(Level.FINEST, "Set of Children : " + chNames);
228:
229: if (!chNames.isEmpty()) {
230: children = new ArrayList(chNames.size());
231: }
232: // Iterate the children to prepare a list of object arrays representing
233: // the child nodes of the current channel
234: Iterator iterator = chNames.iterator();
235: while (iterator.hasNext()) {
236: // Define an Object array representing this child
237: Object[] child = null;
238: String childFQCN = null;
239: String childChannelName = (String) iterator.next();
240: if (channelName == null) {
241: childFQCN = childChannelName;
242: } else {
243: childFQCN = channelName + CHANNEL_NAME_SEPARATOR
244: + childChannelName;
245: }
246:
247: if (!isDisplayable(childFQCN)) {
248: logger.log(Level.FINEST, "Child Not Displayable: "
249: + childFQCN);
250: continue;
251: }
252: // If all channels requested returns everything. Else evaluate the
253: // ones defined at this baseDN.
254: if (!canAccess(childFQCN)) {
255: logger.log(Level.FINEST, "Child Not Accessible: "
256: + childFQCN);
257: continue;
258: }
259:
260: logger.log(Level.FINEST, "Child Channel Name : "
261: + childFQCN);
262:
263: // Check if this channel is a container and if it is get
264: // the list of channels defined in the container
265: DPChannel childChannel = getDPChannel(childFQCN);
266: if (childChannel instanceof DPContainerChannel) {
267: child = processDPNode(childFQCN,
268: TYPE_VISIBLE_CONTAINER_NODE);
269: logger.log(Level.FINEST, "Channel Type : "
270: + TYPE_VISIBLE_CONTAINER_NODE);
271: } else {
272: String type = getChannelType(childFQCN, true);
273: child = processDPNode(childFQCN, type);
274: logger.log(Level.FINEST, "Channel Type : " + type);
275: }
276: children.add(child);
277: }
278:
279: Object[] currNode = { channelName, channelType, children };
280: return currNode;
281: }
282:
283: }
|