psmlmanageraction.java
来自「jetspeed源代码」· Java 代码 · 共 1,653 行 · 第 1/5 页
JAVA
1,653 行
locator = Profiler.createLocator();
if (categoryName.equalsIgnoreCase(Profiler.PARAM_GROUP))
{
locator.setGroupByName(categoryValue);
}
else if (categoryName.equalsIgnoreCase(Profiler.PARAM_ROLE))
{
locator.setRoleByName(categoryValue);
}
else if (categoryName.equalsIgnoreCase(Profiler.PARAM_USER))
{
locator.setUser(JetspeedSecurity.getUser(categoryValue));
}
else
{
locator.setAnonymous(true);
}
String tempVar = rundata.getParameters().getString("MediaType");
if (tempVar != null && tempVar.trim().length() > 0)
{
locator.setMediaType(tempVar);
}
tempVar = rundata.getParameters().getString("Language");
if (tempVar != null && tempVar.trim().length() > 0)
{
locator.setLanguage(tempVar);
}
tempVar = rundata.getParameters().getString("Country");
if (tempVar != null && tempVar.trim().length() > 0)
{
locator.setCountry(tempVar);
}
locator.setName(name);
//
// validate that its not an 'blank' profile -- not allowed
//
if (name == null || name.trim().length() == 0)
{
JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
DynamicURI duri = link.addPathInfo(SecurityConstants.PARAM_MODE,
"import")
.addPathInfo(SecurityConstants.PARAM_MSGID,
SecurityConstants.MID_INVALID_ENTITY_NAME);
JetspeedLinkFactory.putInstance(link);
rundata.setRedirectURI(duri.toString());
//save user entered values
if (locator != null)
{
rundata.getUser().setTemp(TEMP_LOCATOR, locator);
}
if (categoryName != null)
{
rundata.getUser().setTemp(CATEGORY_NAME, categoryName);
}
if (categoryValue != null)
{
rundata.getUser().setTemp(CATEGORY_VALUE, categoryValue);
}
if (copyFrom != null)
{
rundata.getUser().setTemp(COPY_FROM, copyFrom);
}
return;
}
//
// Retrieve the document to import
//
PSMLDocument doc = this.loadDocument(copyFrom);
//
// create a new profile
//
if (doc != null)
{
Portlets portlets = doc.getPortlets();
//
// Profiler does not provide update capability - must remove before replacing
//
if (PsmlManager.getDocument(locator) != null)
{
Profiler.removeProfile(locator);
}
profile = Profiler.createProfile(locator, portlets);
}
else
{
throw new Exception("Failed to load PSML document from disk");
}
rundata.addMessage("Profile for [" + locator.getPath() + "] has been imported from file [" + copyFrom + "]<br>");
setRefreshPsmlFlag(rundata, TRUE);
goBackToBrowser(rundata);
}
catch (Exception e)
{
// log the error msg
logger.error("Exception", e);
//
// dup key found - display error message - bring back to same screen
//
JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
DynamicURI duri = link.addPathInfo(SecurityConstants.PARAM_MODE,
"import")
.addPathInfo(SecurityConstants.PARAM_MSGID,
SecurityConstants.MID_UPDATE_FAILED);
JetspeedLinkFactory.putInstance(link);
rundata.setRedirectURI(duri.toString());
}
// save values that user just entered so they don't have to re-enter
if (locator != null)
{
rundata.getUser().setTemp(TEMP_LOCATOR, locator);
}
if (categoryName != null)
{
rundata.getUser().setTemp(CATEGORY_NAME, categoryName);
}
if (categoryValue != null)
{
rundata.getUser().setTemp(CATEGORY_VALUE, categoryValue);
}
if (copyFrom != null)
{
rundata.getUser().setTemp(COPY_FROM, copyFrom);
}
}
/**
* File Import All Action for Psml.
*
* @param rundata The turbine rundata context for this request.
* @param context The velocity context for this request.
* @exception Exception
*/
public void doImportall(RunData rundata, Context context)
throws Exception
{
String copyFrom = null;
try
{
copyFrom = rundata.getParameters().getString("CopyFrom");
//
// Collect all .psml files from the root specified
//
Vector files = new Vector();
this.collectPsml(files, copyFrom);
//
// Process each file
//
for (Iterator it = files.iterator(); it.hasNext();)
{
// If error occurs processing one entry, continue on with the others
String path = null;
try
{
String psml = ((File) it.next()).getPath();
path = psml.substring(copyFrom.length() + 1);
ProfileLocator locator = this.mapFileToLocator(path);
PSMLDocument doc = this.loadDocument(psml);
//
// create a new profile
//
if (doc != null)
{
Portlets portlets = doc.getPortlets();
//
// Profiler does not provide update capability - must remove before replacing
//
if (PsmlManager.getDocument(locator) != null)
{
Profiler.removeProfile(locator);
}
Portlets clonedPortlets = (Portlets) SerializationUtils.clone(portlets);
org.apache.jetspeed.util.PortletUtils.regenerateIds(clonedPortlets);
Profiler.createProfile(locator, clonedPortlets);
}
else
{
throw new Exception("Failed to load PSML document [" + psml + "] from disk");
}
rundata.addMessage("Profile for [" + locator.getPath() + "] has been imported from file [" + psml + "]<br>");
setRefreshPsmlFlag(rundata, TRUE);
}
catch (Exception ouch)
{
logger.error("Exception", ouch);
rundata.addMessage("ERROR importing file [" + path + "]: " + ouch.toString() + "<br>");
}
}
}
catch (Exception e)
{
// log the error msg
logger.error("Exception", e);
//
// dup key found - display error message - bring back to same screen
//
JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
DynamicURI duri = link.addPathInfo(SecurityConstants.PARAM_MODE,
"import_all")
.addPathInfo(SecurityConstants.PARAM_MSGID,
SecurityConstants.MID_UPDATE_FAILED);
JetspeedLinkFactory.putInstance(link);
rundata.setRedirectURI(duri.toString());
}
// save values that user just entered so they don't have to re-enter
if (copyFrom != null)
{
rundata.getUser().setTemp(COPY_FROM, copyFrom);
}
}
/**
* This method recursively collect all .psml documents starting at the given root
*
* @param v Vector to put the file into
* @param root Root directory for import
*/
private void collectPsml(Vector v, String root)
{
File dir = new File(root);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
{
collectPsml(v, files[i].getPath());
}
else if (files[i].isFile() && files[i].getPath().endsWith(".psml"))
{
v.add(files[i]);
}
}
}
/**
* Creates profile locator from a given path in the format:
*
* user/<name>/<mediaType>/<language>/<country>/<page>/
*
* group/ ""
* role/ ""
*
* @param path The formatted profiler path string.
* @param path fully qualified .psml file name
* @return profile locator
*/
private ProfileLocator mapFileToLocator(String path)
throws Exception
{
if (logger.isDebugEnabled())
{
logger.debug("PsmlUpdateAction.createFromPath: processing path = " + path);
}
ProfileLocator result = Profiler.createLocator();
// Tokenize the file path into elements
StringTokenizer tok = new StringTokenizer(path, File.separator);
// Load path elements into a vector for random access
Vector tokens = new Vector();
while (tok.hasMoreTokens())
{
tokens.add(tok.nextToken());
}
// Assume that 1st element is the profile type (user|role|group) and 2nd is the name
if (tokens.size() > 1)
{
String type = (String) tokens.elementAt(0);
String name = (String) tokens.elementAt(1);
if (type.equals(Profiler.PARAM_USER))
{
result.setUser(JetspeedSecurity.getUser(name));
}
else if (type.equals(Profiler.PARAM_GROUP))
{
result.setGroup(JetspeedSecurity.getGroup(name));
}
else if (type.equals(Profiler.PARAM_ROLE))
{
result.setRole(JetspeedSecurity.getRole(name));
}
}
// Assume that the last element is the page name
if (tokens.size() > 0)
{
result.setName((String) tokens.lastElement());
}
// Based on the number of path elements set the other profile attributes
switch (tokens.size())
{
case 3: // user|role|group/name/page.psml
break;
case 4: // user|role|group/name/media-type/page.psml
result.setMediaType((String) tokens.elementAt(2));
break;
case 5: // user|role|group/name/media-type/language/page.psml
result.setMediaType((String) tokens.elementAt(2));
result.setLanguage((String) tokens.elementAt(3));
break;
case 6: // user|role|group/name/media-type/language/country/page.psml
result.setMediaType((String) tokens.elementAt(2));
result.setLanguage((String) tokens.elementAt(3));
result.setCountry((String) tokens.elementAt(4));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?