Source Code Cross Referenced for KernelServiceBean.java in  » Groupware » LibreSource » org » libresource » kernel » ejb » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Groupware » LibreSource » org.libresource.kernel.ejb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * LibreSource
0003:         * Copyright (C) 2004-2008 Artenum SARL / INRIA
0004:         * http://www.libresource.org - contact@artenum.com
0005:         *
0006:         * This file is part of the LibreSource software, 
0007:         * which can be used and distributed under license conditions.
0008:         * The license conditions are provided in the LICENSE.TXT file 
0009:         * at the root path of the packaging that enclose this file. 
0010:         * More information can be found at 
0011:         * - http://dev.libresource.org/home/license
0012:         *
0013:         * Initial authors :
0014:         *
0015:         * Guillaume Bort / INRIA
0016:         * Francois Charoy / Universite Nancy 2
0017:         * Julien Forest / Artenum
0018:         * Claude Godart / Universite Henry Poincare
0019:         * Florent Jouille / INRIA
0020:         * Sebastien Jourdain / INRIA / Artenum
0021:         * Yves Lerumeur / Artenum
0022:         * Pascal Molli / Universite Henry Poincare
0023:         * Gerald Oster / INRIA
0024:         * Mariarosa Penzi / Artenum
0025:         * Gerard Sookahet / Artenum
0026:         * Raphael Tani / INRIA
0027:         *
0028:         * Contributors :
0029:         *
0030:         * Stephane Bagnier / Artenum
0031:         * Amadou Dia / Artenum-IUP Blois
0032:         * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0033:         */package org.libresource.kernel.ejb;
0034:
0035:        import org.jivesoftware.smack.XMPPConnection;
0036:
0037:        import org.libresource.Libresource;
0038:        import org.libresource.LibresourceEvent;
0039:        import org.libresource.LibresourceException;
0040:        import org.libresource.LibresourceResourceIdentifier;
0041:        import org.libresource.LibresourceResourceValue;
0042:        import org.libresource.LibresourceService;
0043:        import org.libresource.LibresourceServiceBase;
0044:
0045:        import org.libresource.kernel.KernelConstants;
0046:        import org.libresource.kernel.KernelServiceException;
0047:        import org.libresource.kernel.LibresourceSecurityException;
0048:        import org.libresource.kernel.URINotExistException;
0049:        import org.libresource.kernel.ejb.model.AclValue;
0050:        import org.libresource.kernel.ejb.model.SymbolicLinkResourceValue;
0051:        import org.libresource.kernel.interfaces.NodeLocal;
0052:        import org.libresource.kernel.interfaces.NodeLocalHome;
0053:        import org.libresource.kernel.interfaces.SymbolicLinkResourceLocal;
0054:        import org.libresource.kernel.mappers.PrincipalMapper;
0055:        import org.libresource.kernel.util.NodeUtil;
0056:        import org.libresource.kernel.util.SymbolicLinkResourceUtil;
0057:
0058:        import org.libresource.membership.MembershipConstants;
0059:        import org.libresource.membership.ejb.model.ProfileResourceValue;
0060:        import org.libresource.membership.interfaces.MembershipService;
0061:        import org.libresource.membership.interfaces.ProfileResourceLocal;
0062:        import org.libresource.membership.util.ProfileResourceUtil;
0063:
0064:        import org.libresource.scheduler.LibresourceJob;
0065:        import org.libresource.scheduler.LibresourceScheduler;
0066:
0067:        import org.libresource.search.LibresourceSearch;
0068:        import org.libresource.search.LibresourceSearchResult;
0069:
0070:        import org.quartz.CronTrigger;
0071:        import org.quartz.JobDetail;
0072:        import org.quartz.Scheduler;
0073:        import org.quartz.SimpleTrigger;
0074:        import org.quartz.Trigger;
0075:
0076:        import java.io.File;
0077:
0078:        import java.net.URI;
0079:
0080:        import java.util.ArrayList;
0081:        import java.util.Collection;
0082:        import java.util.Collections;
0083:        import java.util.Comparator;
0084:        import java.util.Date;
0085:        import java.util.HashMap;
0086:        import java.util.Hashtable;
0087:        import java.util.Iterator;
0088:        import java.util.Map;
0089:        import java.util.Stack;
0090:        import java.util.StringTokenizer;
0091:        import java.util.Vector;
0092:
0093:        import javax.ejb.ObjectNotFoundException;
0094:
0095:        import javax.mail.Session;
0096:        import javax.mail.Transport;
0097:        import javax.mail.internet.InternetAddress;
0098:        import javax.mail.internet.MimeMessage;
0099:
0100:        import javax.naming.Context;
0101:        import javax.naming.InitialContext;
0102:
0103:        import javax.rmi.PortableRemoteObject;
0104:
0105:        /**
0106:         * LibreSource
0107:         *
0108:         * @author <a href="mailto:bort@loria.fr">Guillaume Bort </a>- <a
0109:         *         href="http://www.inria.fr">INRIA Lorraine </a>
0110:         *
0111:         * The LibreSource Kernel Service
0112:         *
0113:         * @libresource.service name="Kernel"
0114:         *
0115:         * @ejb.env-entry name="uri-scheme" value="ls"
0116:         * @ejb.env-entry name="principal-mapper"
0117:         *                value="org.libresource.kernel.mappers.MembershipPrincipalMapper"
0118:         * @ejb.env-entry name="principal-mapper-options" value=""
0119:         * @ejb.env-entry name="server-config-uri" value="/ServerConfig"
0120:         * @ejb.env-entry name="libresource-version" value="2"
0121:         */
0122:        public abstract class KernelServiceBean extends LibresourceServiceBase {
0123:            private static Object ANONYMOUS = new Object();
0124:            private transient String uriScheme;
0125:            private transient String uriHost;
0126:            private transient String serverUrl;
0127:            private transient String authUrl;
0128:            private transient String libresourceVersion;
0129:            private transient PrincipalMapper principalMapper;
0130:            private transient Hashtable principals = new Hashtable();
0131:            private transient String indexationDataDir;
0132:            private transient URI serverConfigURI;
0133:            private transient Session mailSession;
0134:
0135:            public KernelServiceBean() {
0136:                try {
0137:                    LibresourceScheduler.getInstance();
0138:                } catch (Exception e) {
0139:                }
0140:            }
0141:
0142:            /**
0143:             * @ejb.interface-method
0144:             * @ejb.transaction type="Supports"
0145:             */
0146:            public String getIndexationDataDir() throws KernelServiceException,
0147:                    LibresourceSecurityException {
0148:                try {
0149:                    if (indexationDataDir == null) {
0150:                        // indexationDataDir = (String) new
0151:                        // InitialContext().lookup("java:comp/env/indexation-data-dir");
0152:                        indexationDataDir = org.libresource.Libresource
0153:                                .getLibresourceConfiguration("kernel.indexationDataDir");
0154:
0155:                        if ((indexationDataDir == null)
0156:                                || indexationDataDir.trim().equals("")) {
0157:                            File tempDirectory = new File(System
0158:                                    .getProperty("java.io.tmpdir"),
0159:                                    "libresource-index");
0160:                            tempDirectory.mkdirs();
0161:                            indexationDataDir = tempDirectory.getAbsolutePath();
0162:                        }
0163:                    }
0164:
0165:                    return indexationDataDir;
0166:                } catch (Exception e) {
0167:                    throw new KernelServiceException(
0168:                            "Error in getIndexationDataDir : " + e.getMessage(),
0169:                            e);
0170:                }
0171:            }
0172:
0173:            /**
0174:             * @ejb.interface-method
0175:             * @ejb.transaction type="Supports"
0176:             */
0177:            public URI getServerConfigURI() throws KernelServiceException,
0178:                    LibresourceSecurityException {
0179:                try {
0180:                    if (serverConfigURI == null) {
0181:                        serverConfigURI = normalizeURI(new URI(
0182:                                (String) new InitialContext()
0183:                                        .lookup("java:comp/env/server-config-uri")));
0184:                    }
0185:
0186:                    return serverConfigURI;
0187:                } catch (Exception e) {
0188:                    throw new KernelServiceException(
0189:                            "Error in getServerConfigURI : " + e.getMessage(),
0190:                            e);
0191:                }
0192:            }
0193:
0194:            /**
0195:             * @ejb.interface-method
0196:             * @ejb.transaction type="Required"
0197:             */
0198:            public void scheduleJob(LibresourceJob job, String description,
0199:                    Date date) throws KernelServiceException {
0200:                try {
0201:                    // get scheduler
0202:                    Scheduler scheduler = LibresourceScheduler.getInstance()
0203:                            .getScheduler();
0204:
0205:                    // remove old job with same name ...
0206:                    scheduler.deleteJob(job.getName(), Scheduler.DEFAULT_GROUP);
0207:
0208:                    // create new job
0209:                    JobDetail jobDetail = LibresourceScheduler.createJobDetail(
0210:                            job, description);
0211:
0212:                    // create trigger
0213:                    SimpleTrigger trigger = new SimpleTrigger("trigger."
0214:                            + job.getName(), Scheduler.DEFAULT_GROUP, date,
0215:                            null, 0, 0L);
0216:
0217:                    // schedule job
0218:                    scheduler.scheduleJob(jobDetail, trigger);
0219:                } catch (Exception e) {
0220:                    ctx.setRollbackOnly();
0221:                    throw new KernelServiceException("Error in scheduleJob "
0222:                            + e.getMessage(), e);
0223:                }
0224:            }
0225:
0226:            /**
0227:             * @ejb.interface-method
0228:             * @ejb.transaction type="Required"
0229:             */
0230:            public void scheduleJob(LibresourceJob job, String description,
0231:                    String cronExpression) throws KernelServiceException {
0232:                try {
0233:                    // get scheduler
0234:                    Scheduler scheduler = LibresourceScheduler.getInstance()
0235:                            .getScheduler();
0236:
0237:                    // remove old job with same name ...
0238:                    scheduler.deleteJob(job.getName(), Scheduler.DEFAULT_GROUP);
0239:
0240:                    // create new job
0241:                    JobDetail jobDetail = LibresourceScheduler.createJobDetail(
0242:                            job, description);
0243:
0244:                    // create trigger
0245:                    CronTrigger trigger = new CronTrigger("trigger."
0246:                            + job.getName(), Scheduler.DEFAULT_GROUP,
0247:                            cronExpression);
0248:                    trigger
0249:                            .setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
0250:
0251:                    // schedule job
0252:                    scheduler.scheduleJob(jobDetail, trigger);
0253:                } catch (Exception e) {
0254:                    ctx.setRollbackOnly();
0255:                    throw new KernelServiceException("Error in scheduleJob "
0256:                            + e.getMessage(), e);
0257:                }
0258:            }
0259:
0260:            /**
0261:             * @ejb.interface-method
0262:             * @ejb.transaction type="Required"
0263:             */
0264:            public void unScheduleJob(String jobName)
0265:                    throws KernelServiceException {
0266:                try {
0267:                    // get scheduler
0268:                    Scheduler scheduler = LibresourceScheduler.getInstance()
0269:                            .getScheduler();
0270:
0271:                    // remove old job with same name ...
0272:                    scheduler.deleteJob(jobName, Scheduler.DEFAULT_GROUP);
0273:                } catch (Exception e) {
0274:                    ctx.setRollbackOnly();
0275:                    throw new KernelServiceException("Error in unScheduleJob "
0276:                            + e.getMessage(), e);
0277:                }
0278:            }
0279:
0280:            /**
0281:             * @ejb.interface-method
0282:             * @ejb.transaction type="Supports"
0283:             */
0284:            public String[] listScheduledJobs() throws KernelServiceException {
0285:                try {
0286:                    // get scheduler
0287:                    Scheduler scheduler = LibresourceScheduler.getInstance()
0288:                            .getScheduler();
0289:
0290:                    // remove old job with same name ...
0291:                    return scheduler.getJobNames(Scheduler.DEFAULT_GROUP);
0292:                } catch (Exception e) {
0293:                    throw new KernelServiceException(
0294:                            "Error in listScheduledJobs " + e.getMessage(), e);
0295:                }
0296:            }
0297:
0298:            /**
0299:             * @ejb.interface-method
0300:             * @ejb.transaction type="Supports"
0301:             */
0302:            public String getJobDescription(String jobName)
0303:                    throws KernelServiceException {
0304:                try {
0305:                    // get scheduler
0306:                    Scheduler scheduler = LibresourceScheduler.getInstance()
0307:                            .getScheduler();
0308:
0309:                    // remove old job with same name ...
0310:                    return scheduler.getJobDetail(jobName,
0311:                            Scheduler.DEFAULT_GROUP).getDescription();
0312:                } catch (Exception e) {
0313:                    throw new KernelServiceException(
0314:                            "Error in getJobDescription " + e.getMessage(), e);
0315:                }
0316:            }
0317:
0318:            /**
0319:             * @ejb.interface-method
0320:             * @ejb.transaction type="Supports"
0321:             */
0322:            public String getTriggersInformation(String jobName)
0323:                    throws KernelServiceException {
0324:                try {
0325:                    // get scheduler
0326:                    Scheduler scheduler = LibresourceScheduler.getInstance()
0327:                            .getScheduler();
0328:
0329:                    // remove old job with same name ...
0330:                    Trigger[] triggers = scheduler.getTriggersOfJob(jobName,
0331:                            Scheduler.DEFAULT_GROUP);
0332:
0333:                    if (triggers.length > 0) {
0334:                        if (triggers[0] instanceof  SimpleTrigger) {
0335:                            SimpleTrigger theTrigger = (SimpleTrigger) triggers[0];
0336:
0337:                            return "Fire at " + theTrigger.getNextFireTime();
0338:                        } else if (triggers[0] instanceof  CronTrigger) {
0339:                            CronTrigger theTrigger = (CronTrigger) triggers[0];
0340:
0341:                            return "Cron :  " + theTrigger.getCronExpression()
0342:                                    + " (next fire at "
0343:                                    + theTrigger.getNextFireTime() + ")";
0344:                        }
0345:                    }
0346:
0347:                    return "No informations";
0348:                } catch (Exception e) {
0349:                    throw new KernelServiceException(
0350:                            "Error in getTriggersInformation " + e.getMessage(),
0351:                            e);
0352:                }
0353:            }
0354:
0355:            /**
0356:             * @ejb.interface-method
0357:             * @ejb.transaction type="Supports"
0358:             */
0359:            public String getUriScheme() throws KernelServiceException {
0360:                try {
0361:                    if (uriScheme == null) {
0362:                        uriScheme = (String) new InitialContext()
0363:                                .lookup("java:comp/env/uri-scheme");
0364:                    }
0365:
0366:                    return uriScheme;
0367:                } catch (Exception e) {
0368:                    throw new KernelServiceException("Error in getUriScheme "
0369:                            + e.getMessage(), e);
0370:                }
0371:            }
0372:
0373:            /**
0374:             * @ejb.interface-method
0375:             * @ejb.transaction type="Supports"
0376:             */
0377:            public String getServerUrl() throws KernelServiceException {
0378:                try {
0379:                    if (serverUrl == null) {
0380:                        // serverUrl = (String) new
0381:                        // InitialContext().lookup("java:comp/env/server-url");
0382:                        serverUrl = org.libresource.Libresource
0383:                                .getLibresourceConfiguration("kernel.serverUrl");
0384:                    }
0385:
0386:                    return serverUrl;
0387:                } catch (Exception e) {
0388:                    throw new KernelServiceException("Error in getServerUrl "
0389:                            + e.getMessage(), e);
0390:                }
0391:            }
0392:
0393:            /**
0394:             * @ejb.interface-method
0395:             * @ejb.transaction type="Supports"
0396:             */
0397:            public String getUriHost() throws KernelServiceException {
0398:                try {
0399:                    if (uriHost == null) {
0400:                        uriHost = new URI(getServerUrl()).getHost();
0401:
0402:                        int port = new URI(getServerUrl()).getPort();
0403:
0404:                        if (port != -1) {
0405:                            uriHost = uriHost + ":" + port;
0406:                        }
0407:                    }
0408:
0409:                    return uriHost;
0410:                } catch (Exception e) {
0411:                    throw new KernelServiceException("Error in getUriHost "
0412:                            + e.getMessage(), e);
0413:                }
0414:            }
0415:
0416:            /**
0417:             * @ejb.interface-method
0418:             * @ejb.transaction type="Supports"
0419:             */
0420:            public String getAuthUrl() throws KernelServiceException {
0421:                try {
0422:                    if (authUrl == null) {
0423:                        // authUrl = (String) new
0424:                        // InitialContext().lookup("java:comp/env/auth-url");
0425:                        authUrl = org.libresource.Libresource
0426:                                .getLibresourceConfiguration("kernel.authUrl");
0427:                    }
0428:
0429:                    return authUrl;
0430:                } catch (Exception e) {
0431:                    throw new KernelServiceException("Error in getAuthUrl "
0432:                            + e.getMessage(), e);
0433:                }
0434:            }
0435:
0436:            /**
0437:             * @ejb.interface-method
0438:             * @ejb.transaction type="Supports"
0439:             */
0440:            public String getLibresourceVersion() throws KernelServiceException {
0441:                try {
0442:                    if (libresourceVersion == null) {
0443:                        libresourceVersion = (String) new InitialContext()
0444:                                .lookup("java:comp/env/libresource-version");
0445:                    }
0446:
0447:                    return libresourceVersion;
0448:                } catch (Exception e) {
0449:                    throw new KernelServiceException(
0450:                            "Error in getLibresourceVersion " + e.getMessage(),
0451:                            e);
0452:                }
0453:            }
0454:
0455:            // mails
0456:
0457:            /**
0458:             * @ejb.interface-method
0459:             * @ejb.transaction type="Supports"
0460:             */
0461:            public Session getMailSession() throws Exception {
0462:                if (mailSession == null) {
0463:                    mailSession = (Session) PortableRemoteObject
0464:                            .narrow(
0465:                                    new InitialContext()
0466:                                            .lookup(Libresource
0467:                                                    .getLibresourceConfiguration("mail.lookup")),
0468:                                    Session.class);
0469:                }
0470:
0471:                return mailSession;
0472:            }
0473:
0474:            /**
0475:             * @ejb.interface-method
0476:             * @ejb.transaction type="Supports"
0477:             */
0478:            public void sendEmail(String userId, String mail, String subject)
0479:                    throws Exception {
0480:                // TODO check if profile exist
0481:                MembershipService membershipService = (MembershipService) Libresource
0482:                        .getService(MembershipConstants.SERVICE);
0483:                ProfileResourceValue user = membershipService
0484:                        .systemGetProfile(new URI(membershipService
0485:                                .getUsersRootURI()
0486:                                + "/" + userId));
0487:                MimeMessage m = new MimeMessage(getMailSession());
0488:                m.setFrom();
0489:                m.setRecipients(javax.mail.Message.RecipientType.TO, user
0490:                        .getEmail());
0491:                m.setSubject(subject);
0492:                m.setSentDate(new java.util.Date());
0493:                m.setContent("For user " + user.getFullName() + "\r\n\r\n"
0494:                        + mail, "text/plain");
0495:                Transport.send(m);
0496:            }
0497:
0498:            /**
0499:             * @ejb.interface-method
0500:             * @ejb.transaction type="Supports"
0501:             */
0502:            public void sendEmail(String from, String userId, String mail,
0503:                    String subject) throws Exception {
0504:                // TODO check if profile exist
0505:                MembershipService membershipService = (MembershipService) Libresource
0506:                        .getService(MembershipConstants.SERVICE);
0507:                ProfileResourceValue user = membershipService
0508:                        .systemGetProfile(new URI(membershipService
0509:                                .getUsersRootURI()
0510:                                + "/" + userId));
0511:                MimeMessage m = new MimeMessage(getMailSession());
0512:                m.setFrom(new InternetAddress(from));
0513:                m.setRecipients(javax.mail.Message.RecipientType.TO, user
0514:                        .getEmail());
0515:                m.setSubject(subject);
0516:                m.setSentDate(new java.util.Date());
0517:                m.setContent("For user " + user.getFullName() + "\r\n\r\n"
0518:                        + mail, "text/plain");
0519:                Transport.send(m);
0520:            }
0521:
0522:            // nodes management
0523:
0524:            /**
0525:             * @ejb.interface-method
0526:             * @ejb.transaction type="Required"
0527:             */
0528:            public void bind(LibresourceResourceIdentifier resourceIdentifier,
0529:                    URI uri, String resourceName)
0530:                    throws KernelServiceException,
0531:                    LibresourceSecurityException, URINotExistException {
0532:                try {
0533:                    createURI(uri);
0534:
0535:                    if (!checkIfCanChangeRights(uri)) {
0536:                        throw new LibresourceSecurityException(uri,
0537:                                "OWNER or CRUD");
0538:                    }
0539:
0540:                    String path = normalizeAbsoluteURIPath(uri);
0541:
0542:                    try {
0543:                        try {
0544:                            NodeUtil.getLocalHome().findByBindedResource(
0545:                                    resourceIdentifier.serialize());
0546:                            throw new Exception(
0547:                                    "This resource is already binded with another URI.");
0548:                        } catch (ObjectNotFoundException e) {
0549:                            //
0550:                        }
0551:
0552:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
0553:                                .getByPath(path);
0554:
0555:                        if (pathNodeLocal.getBindedResourceIdentifier() != null) {
0556:                            throw new Exception(
0557:                                    "A resource is already binded with this URI "
0558:                                            + uri);
0559:                        }
0560:
0561:                        pathNodeLocal
0562:                                .setBindedResourceIdentifier(resourceIdentifier
0563:                                        .serialize());
0564:
0565:                        // filter ACLs
0566:                        HashMap acls = pathNodeLocal.getAcls();
0567:
0568:                        for (Iterator i = acls.keySet().iterator(); i.hasNext();) {
0569:                            URI key = (URI) i.next();
0570:                            String[] permissions = (String[]) acls.get(key);
0571:                            acls.put(key, filterPermissions(resourceIdentifier,
0572:                                    permissions));
0573:                        }
0574:
0575:                        pathNodeLocal.setAcls(acls);
0576:                        pathNodeLocal.setShortName(resourceName);
0577:                        pathNodeLocal.setUpdateDate(new Date());
0578:                    } catch (ObjectNotFoundException e) {
0579:                        throw new URINotExistException(normalizeURI(uri));
0580:                    }
0581:
0582:                    // indexation
0583:                    Libresource.index(uri);
0584:
0585:                    // event
0586:                    Libresource.throwEvent(new LibresourceEvent(uri,
0587:                            resourceIdentifier, getConnectedResource(),
0588:                            KernelConstants.EVENT_BIND));
0589:                } catch (LibresourceSecurityException se) {
0590:                    ctx.setRollbackOnly();
0591:                    throw se;
0592:                } catch (URINotExistException se) {
0593:                    ctx.setRollbackOnly();
0594:                    throw se;
0595:                } catch (Exception e) {
0596:                    ctx.setRollbackOnly();
0597:                    throw new KernelServiceException("Error in bind : "
0598:                            + e.getMessage(), e);
0599:                }
0600:            }
0601:
0602:            /**
0603:             * @ejb.interface-method
0604:             * @ejb.transaction type="Required"
0605:             */
0606:            public void move(URI from, URI to) throws KernelServiceException,
0607:                    LibresourceSecurityException, URINotExistException {
0608:                try {
0609:                    if (exist(to)) {
0610:                        throw new Exception("URI " + to + " already exists");
0611:                    }
0612:
0613:                    if (isRoot(from)) {
0614:                        throw new Exception("Can't move the root node");
0615:                    }
0616:
0617:                    String fromPath = normalizeAbsoluteURIPath(from);
0618:                    String toPath = normalizeAbsoluteURIPath(to);
0619:
0620:                    if (toPath.startsWith(fromPath + "/")) {
0621:                        throw new Exception("Cannot move URI " + from
0622:                                + " to a child node of itself");
0623:                    }
0624:
0625:                    String toPathParent = toPath.substring(0, toPath
0626:                            .lastIndexOf("/"));
0627:                    String toPathLastNode = toPath.substring(toPath
0628:                            .lastIndexOf("/") + 1, toPath.length());
0629:
0630:                    if (toPathParent.equals("")) {
0631:                        toPathParent = "/";
0632:                    }
0633:
0634:                    NodeLocal fromNode;
0635:                    NodeLocal toParentNode;
0636:
0637:                    try {
0638:                        fromNode = NodeUtil.getLocalHome().getByPath(fromPath);
0639:                    } catch (ObjectNotFoundException e) {
0640:                        throw new URINotExistException(normalizeURI(from));
0641:                    }
0642:
0643:                    String oldPathPart = fromNode.getPathPart();
0644:
0645:                    if (!checkSecurity(new URI(toPathParent),
0646:                            KernelConstants.SECURITY_CREATE)) {
0647:                        throw new LibresourceSecurityException(new URI(
0648:                                toPathParent), KernelConstants.SECURITY_CREATE);
0649:                    }
0650:
0651:                    if (!checkSecurity(from, KernelConstants.SECURITY_DELETE)) {
0652:                        throw new LibresourceSecurityException(from,
0653:                                KernelConstants.SECURITY_DELETE);
0654:                    }
0655:
0656:                    createURI(new URI(toPathParent));
0657:                    toParentNode = NodeUtil.getLocalHome().getByPath(
0658:                            toPathParent);
0659:
0660:                    NodeLocal parent = NodeUtil.getLocalHome()
0661:                            .findByPrimaryKey(fromNode.getParent());
0662:                    fromNode.setParent(toParentNode.getId());
0663:                    fromNode.setPathPart(toPathLastNode);
0664:                    toParentNode.addChild(fromNode);
0665:                    parent.removeChild(oldPathPart);
0666:                    fromNode.updatePath();
0667:
0668:                    // event
0669:                    Libresource.throwEvent(new LibresourceEvent(from,
0670:                            KernelConstants.SERVICE, "Node",
0671:                            getConnectedResource(),
0672:                            KernelConstants.EVENT_MOVE_FROM));
0673:                    Libresource.throwEvent(new LibresourceEvent(to,
0674:                            KernelConstants.SERVICE, "Node",
0675:                            getConnectedResource(),
0676:                            KernelConstants.EVENT_MOVE_TO));
0677:                } catch (LibresourceSecurityException se) {
0678:                    ctx.setRollbackOnly();
0679:                    throw se;
0680:                } catch (URINotExistException se) {
0681:                    ctx.setRollbackOnly();
0682:                    throw se;
0683:                } catch (Exception e) {
0684:                    ctx.setRollbackOnly();
0685:                    throw new KernelServiceException("Error in move : "
0686:                            + e.getMessage(), e);
0687:                }
0688:            }
0689:
0690:            /**
0691:             * @ejb.interface-method
0692:             * @ejb.transaction type="Supports"
0693:             */
0694:            public boolean exist(URI uri) throws KernelServiceException,
0695:                    LibresourceSecurityException {
0696:                try {
0697:                    String path = normalizeAbsoluteURIPath(uri);
0698:
0699:                    try {
0700:                        NodeUtil.getLocalHome().getByPath(path);
0701:                    } catch (ObjectNotFoundException e) {
0702:                        return false;
0703:                    }
0704:
0705:                    return true;
0706:                } catch (Exception e) {
0707:                    throw new KernelServiceException("Error in exist "
0708:                            + e.getMessage(), e);
0709:                }
0710:            }
0711:
0712:            /**
0713:             * @ejb.interface-method
0714:             * @ejb.transaction type="Required"
0715:             */
0716:            public void unbind(URI uri) throws KernelServiceException,
0717:                    LibresourceSecurityException, URINotExistException {
0718:                try {
0719:                    String path = normalizeAbsoluteURIPath(uri);
0720:
0721:                    try {
0722:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
0723:                                .getByPath(path);
0724:
0725:                        if (!checkIfCanChangeRights(uri)) {
0726:                            throw new LibresourceSecurityException(uri,
0727:                                    "OWNER or CRUD");
0728:                        }
0729:
0730:                        pathNodeLocal.setBindedResourceIdentifier(null);
0731:                        pathNodeLocal.setShortName("(empty node)");
0732:                        pathNodeLocal.setUpdateDate(new Date());
0733:                    } catch (ObjectNotFoundException e) {
0734:                        throw new URINotExistException(normalizeURI(uri));
0735:                    }
0736:
0737:                    // indexation
0738:                    Libresource.removeFromIndex(uri);
0739:
0740:                    // event
0741:                    Libresource.throwEvent(new LibresourceEvent(uri,
0742:                            KernelConstants.SERVICE, "Node",
0743:                            getConnectedResource(),
0744:                            KernelConstants.EVENT_UNBIND));
0745:                } catch (LibresourceSecurityException se) {
0746:                    ctx.setRollbackOnly();
0747:                    throw se;
0748:                } catch (URINotExistException se) {
0749:                    ctx.setRollbackOnly();
0750:                    throw se;
0751:                } catch (Exception e) {
0752:                    ctx.setRollbackOnly();
0753:                    throw new KernelServiceException("Error in unbind : "
0754:                            + e.getMessage(), e);
0755:                }
0756:            }
0757:
0758:            /**
0759:             * @ejb.interface-method
0760:             * @ejb.transaction type="Supports"
0761:             */
0762:            public LibresourceResourceIdentifier lookup(URI uri)
0763:                    throws KernelServiceException,
0764:                    LibresourceSecurityException, URINotExistException {
0765:                try {
0766:                    String path = normalizeAbsoluteURIPath(uri);
0767:
0768:                    try {
0769:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
0770:                                .getByPath(path);
0771:
0772:                        /*
0773:                         * if (!checkSecurity(uri,
0774:                         * KernelConstants.SECURITY_KERNEL_READ)) { throw new
0775:                         * LibresourceSecurityException(uri,
0776:                         * KernelConstants.SECURITY_KERNEL_READ); }
0777:                         */
0778:                        if (pathNodeLocal.getBindedResourceIdentifier() == null) {
0779:                            return null;
0780:                        }
0781:
0782:                        return LibresourceResourceIdentifier
0783:                                .deserialize(pathNodeLocal
0784:                                        .getBindedResourceIdentifier());
0785:                    } catch (ObjectNotFoundException e) {
0786:                        throw new URINotExistException(normalizeURI(uri)
0787:                                + " :: " + e.getMessage());
0788:                    }
0789:                } catch (LibresourceSecurityException se) {
0790:                    throw se;
0791:                } catch (URINotExistException se) {
0792:                    throw se;
0793:                } catch (Exception e) {
0794:                    throw new KernelServiceException("Error in lookup : "
0795:                            + e.getMessage(), e);
0796:                }
0797:            }
0798:
0799:            /**
0800:             * @ejb.interface-method
0801:             * @ejb.transaction type="Supports"
0802:             */
0803:            public String getShortName(URI uri) throws KernelServiceException,
0804:                    LibresourceSecurityException, URINotExistException {
0805:                try {
0806:                    String path = normalizeAbsoluteURIPath(uri);
0807:
0808:                    try {
0809:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
0810:                                .getByPath(path);
0811:
0812:                        // if (!checkSecurity(uri, "Kernel:READ")) {
0813:                        // return path.substring(path.lastIndexOf("/") + 1,
0814:                        // path.length());
0815:                        // }
0816:                        return pathNodeLocal.getShortName();
0817:                    } catch (ObjectNotFoundException e) {
0818:                        throw new URINotExistException(normalizeURI(uri)
0819:                                + " :: " + e.getMessage());
0820:                    }
0821:                } catch (LibresourceSecurityException se) {
0822:                    throw se;
0823:                } catch (URINotExistException se) {
0824:                    throw se;
0825:                } catch (Exception e) {
0826:                    throw new KernelServiceException("Error in lookup : "
0827:                            + e.getMessage(), e);
0828:                }
0829:            }
0830:
0831:            /**
0832:             * @ejb.interface-method
0833:             * @ejb.transaction type="Required"
0834:             */
0835:            public void setShortName(URI uri, String name)
0836:                    throws KernelServiceException,
0837:                    LibresourceSecurityException, URINotExistException {
0838:                try {
0839:                    String path = normalizeAbsoluteURIPath(uri);
0840:
0841:                    try {
0842:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
0843:                                .getByPath(path);
0844:
0845:                        // Shouldn't be needed
0846:                        /*
0847:                         * if (!checkSecurity(uri,
0848:                         * KernelConstants.SECURITY_KERNEL_UPDATE)) { throw new
0849:                         * LibresourceSecurityException(uri,
0850:                         * KernelConstants.SECURITY_KERNEL_UPDATE); }
0851:                         */
0852:                        pathNodeLocal.setShortName(name);
0853:                    } catch (ObjectNotFoundException e) {
0854:                        throw new URINotExistException(normalizeURI(uri)
0855:                                + " :: " + e.getMessage());
0856:                    }
0857:                } catch (LibresourceSecurityException se) {
0858:                    throw se;
0859:                } catch (URINotExistException se) {
0860:                    throw se;
0861:                } catch (Exception e) {
0862:                    throw new KernelServiceException("Error in setShortName : "
0863:                            + e.getMessage(), e);
0864:                }
0865:            }
0866:
0867:            /**
0868:             * @ejb.interface-method
0869:             * @ejb.transaction type="Supports"
0870:             */
0871:            public URI getOwner(URI uri) throws KernelServiceException,
0872:                    LibresourceSecurityException, URINotExistException {
0873:                try {
0874:                    String path = normalizeAbsoluteURIPath(uri);
0875:
0876:                    try {
0877:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
0878:                                .getByPath(path);
0879:
0880:                        if (!checkSecurity(uri, KernelConstants.SECURITY_READ)) {
0881:                            throw new LibresourceSecurityException(uri,
0882:                                    KernelConstants.SECURITY_READ);
0883:                        }
0884:
0885:                        if (pathNodeLocal.getOwner().trim().length() == 0) {
0886:                            return null;
0887:                        }
0888:
0889:                        NodeLocal owner = NodeUtil.getLocalHome()
0890:                                .findByPrimaryKey(pathNodeLocal.getOwner());
0891:
0892:                        if (owner == null) {
0893:                            return null;
0894:                        }
0895:
0896:                        return normalizeURI(owner.getURI());
0897:                    } catch (ObjectNotFoundException e) {
0898:                        throw new URINotExistException(normalizeURI(uri));
0899:                    }
0900:                } catch (LibresourceSecurityException se) {
0901:                    throw se;
0902:                } catch (URINotExistException se) {
0903:                    throw se;
0904:                } catch (Exception e) {
0905:                    throw new KernelServiceException("Error in getOwner : "
0906:                            + e.getMessage(), e);
0907:                }
0908:            }
0909:
0910:            /**
0911:             * @ejb.interface-method
0912:             * @ejb.transaction type="Required"
0913:             */
0914:            public void deleteURI(URI uri) throws KernelServiceException,
0915:                    LibresourceSecurityException, URINotExistException {
0916:                try {
0917:                    String path = normalizeAbsoluteURIPath(uri);
0918:
0919:                    if (isRoot(uri)) {
0920:                        throw new Exception("Can't delete the root node");
0921:                    }
0922:
0923:                    try {
0924:                        NodeLocal nodeLocal = NodeUtil.getLocalHome()
0925:                                .getByPath(path);
0926:
0927:                        if (!checkSecurity(uri, KernelConstants.SECURITY_DELETE)) {
0928:                            throw new LibresourceSecurityException(uri,
0929:                                    KernelConstants.SECURITY_DELETE);
0930:                        }
0931:
0932:                        // event
0933:                        Libresource.throwEvent(new LibresourceEvent(uri,
0934:                                KernelConstants.SERVICE, "Node",
0935:                                getConnectedResource(),
0936:                                KernelConstants.EVENT_DELETE));
0937:                        nodeLocal.remove();
0938:
0939:                        /*
0940:                         * Collection children = pathNodeLocal.getChildren2(); for
0941:                         * (Iterator i = children.iterator(); i.hasNext();) {
0942:                         * deleteURI(((NodeLocal) i.next()).getURI()); }
0943:                         *
0944:                         * NodeUtil.getLocalHome().getByPath(pathNodeLocal.getParent()).removeChild(pathNodeLocal.getId());
0945:                         * nodeLocal.remove();
0946:                         */
0947:                    } catch (ObjectNotFoundException e) {
0948:                        throw new URINotExistException(normalizeURI(uri));
0949:                    }
0950:                } catch (LibresourceSecurityException se) {
0951:                    ctx.setRollbackOnly();
0952:                    throw se;
0953:                } catch (URINotExistException se) {
0954:                    ctx.setRollbackOnly();
0955:                    throw se;
0956:                } catch (Exception e) {
0957:                    ctx.setRollbackOnly();
0958:                    throw new KernelServiceException("Error in deleteURI : "
0959:                            + e.getMessage(), e);
0960:                }
0961:            }
0962:
0963:            /**
0964:             * @ejb.interface-method
0965:             * @ejb.transaction type="Required"
0966:             */
0967:            public void createURI(URI uri) throws KernelServiceException,
0968:                    LibresourceSecurityException {
0969:                try {
0970:                    NodeLocalHome pathNodeLocalHome = NodeUtil.getLocalHome();
0971:                    StringTokenizer tokenizer = new StringTokenizer(
0972:                            normalizeAbsoluteURIPath(uri), "/");
0973:                    ArrayList pathParts = new ArrayList();
0974:
0975:                    while (tokenizer.hasMoreTokens()) {
0976:                        pathParts.add(tokenizer.nextToken());
0977:                    }
0978:
0979:                    NodeLocal tempParent = null;
0980:
0981:                    try {
0982:                        tempParent = pathNodeLocalHome.findByPrimaryKey("666");
0983:                    } catch (ObjectNotFoundException e) {
0984:                        throw new KernelServiceException("root node must exist");
0985:                    }
0986:
0987:                    for (Iterator i = pathParts.iterator(); i.hasNext();) {
0988:                        String pathPart = (String) i.next();
0989:
0990:                        // tempPath.append(pathPart);
0991:                        String childid = (String) tempParent.getChildren().get(
0992:                                pathPart);
0993:
0994:                        if (childid == null) {
0995:                            if (!checkSecurity(tempParent.getURI(),
0996:                                    KernelConstants.SECURITY_CREATE)) {
0997:                                throw new LibresourceSecurityException(
0998:                                        tempParent.getURI(),
0999:                                        KernelConstants.SECURITY_CREATE);
1000:                            }
1001:
1002:                            NodeLocal newNode = pathNodeLocalHome.create(
1003:                                    tempParent, pathPart);
1004:                            copyAcls(tempParent, newNode);
1005:                            tempParent.addChild(newNode);
1006:                            tempParent = newNode;
1007:
1008:                            // try {
1009:                            newNode
1010:                                    .setOwner(NodeUtil
1011:                                            .getLocalHome()
1012:                                            .getByPath(
1013:                                                    normalizeAbsoluteURIPath(getConnectedResource()))
1014:                                            .getId());
1015:
1016:                            // newNode.setOwner(getConnectedResource().getId());
1017:                            // } catch (ObjectNotFoundException onfe) {
1018:                            // throw new KernelServiceException("Can't set this owner
1019:                            // (not found) : " + getConnectedResource());
1020:                            // }
1021:                        } else {
1022:                            tempParent = pathNodeLocalHome
1023:                                    .findByPrimaryKey(childid);
1024:                        }
1025:                    }
1026:
1027:                    // event
1028:                    Libresource.throwEvent(new LibresourceEvent(uri,
1029:                            KernelConstants.SERVICE, "Node",
1030:                            getConnectedResource(),
1031:                            KernelConstants.EVENT_CREATE));
1032:                } catch (KernelServiceException e) {
1033:                    ctx.setRollbackOnly();
1034:                    throw e;
1035:                } catch (LibresourceSecurityException e) {
1036:                    ctx.setRollbackOnly();
1037:                    throw e;
1038:                } catch (Exception e) {
1039:                    ctx.setRollbackOnly();
1040:                    throw new KernelServiceException("Error in createURI : "
1041:                            + e.getMessage(), e);
1042:                }
1043:            }
1044:
1045:            /*
1046:             * private NodeLocal createNode(NodeLocal parent, String path) throws
1047:             * KernelServiceException, LibresourceSecurityException,
1048:             * URINotExistException { try { return
1049:             * NodeUtil.getLocalHome().create(parent, path); } catch (Exception e) {
1050:             * throw new KernelServiceException("Error in getParent : " +
1051:             * e.getMessage(), e); } }
1052:             */
1053:
1054:            /**
1055:             * @ejb.interface-method
1056:             * @ejb.transaction type="Supports"
1057:             */
1058:            public URI getParent(URI uri) throws KernelServiceException,
1059:                    LibresourceSecurityException, URINotExistException {
1060:                try {
1061:                    try {
1062:                        NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
1063:                                .getByPath(normalizeAbsoluteURIPath(uri));
1064:
1065:                        if (pathNodeLocal.getParent().trim().length() == 0) {
1066:                            return null;
1067:                        }
1068:
1069:                        NodeLocal parent = NodeUtil.getLocalHome()
1070:                                .findByPrimaryKey(pathNodeLocal.getParent());
1071:
1072:                        if (parent == null) {
1073:                            return null;
1074:                        }
1075:
1076:                        return normalizeURI(parent.getURI());
1077:                    } catch (ObjectNotFoundException e) {
1078:                        throw new URINotExistException(normalizeURI(uri));
1079:                    }
1080:                } catch (URINotExistException se) {
1081:                    throw se;
1082:                } catch (Exception e) {
1083:                    throw new KernelServiceException("Error in getParent : "
1084:                            + e.getMessage(), e);
1085:                }
1086:            }
1087:
1088:            /**
1089:             * @ejb.interface-method
1090:             * @ejb.transaction type="Supports"
1091:             */
1092:            public URI getMyURI(LibresourceResourceIdentifier resourceIdentifier)
1093:                    throws KernelServiceException,
1094:                    LibresourceSecurityException, URINotExistException {
1095:                try {
1096:                    NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
1097:                            .findByBindedResource(
1098:                                    resourceIdentifier.serialize());
1099:
1100:                    return normalizeURI(pathNodeLocal.getURI());
1101:                } catch (ObjectNotFoundException e) {
1102:                    throw new KernelServiceException(
1103:                            "This resource is not binded");
1104:                } catch (URINotExistException se) {
1105:                    throw se;
1106:                } catch (Exception e) {
1107:                    throw new KernelServiceException("Error in getMyURI : "
1108:                            + e.getMessage(), e);
1109:                }
1110:            }
1111:
1112:            /**
1113:             * @ejb.interface-method
1114:             * @ejb.transaction type="Supports"
1115:             */
1116:            public boolean isRoot(URI uri) throws KernelServiceException,
1117:                    LibresourceSecurityException, URINotExistException {
1118:                try {
1119:                    return normalizeAbsoluteURIPath(uri).equals("/");
1120:                } catch (URINotExistException se) {
1121:                    throw se;
1122:                } catch (Exception e) {
1123:                    throw new KernelServiceException("Error in isRoot : "
1124:                            + e.getMessage(), e);
1125:                }
1126:            }
1127:
1128:            /**
1129:             * @ejb.interface-method
1130:             * @ejb.transaction type="Supports"
1131:             */
1132:            public URI normalizeURI(URI uri) throws KernelServiceException,
1133:                    LibresourceSecurityException, URINotExistException {
1134:                try {
1135:                    return new URI(getUriScheme() + "://" + getUriHost()
1136:                            + normalizeAbsoluteURIPath(uri));
1137:                } catch (URINotExistException se) {
1138:                    throw se;
1139:                } catch (Exception e) {
1140:                    throw new KernelServiceException("Error in normalizeURI : "
1141:                            + e.getMessage(), e);
1142:                }
1143:            }
1144:
1145:            /**
1146:             * @ejb.interface-method
1147:             * @ejb.transaction type="Required"
1148:             */
1149:            public void chown(URI uri, URI owner, boolean recursive)
1150:                    throws KernelServiceException,
1151:                    LibresourceSecurityException, URINotExistException {
1152:                try {
1153:                    if (!checkIfCanChangeRights(uri)) {
1154:                        throw new LibresourceSecurityException(uri,
1155:                                "OWNER or CRUD");
1156:                    }
1157:
1158:                    systemChown(uri, owner, recursive);
1159:                } catch (LibresourceSecurityException se) {
1160:                    ctx.setRollbackOnly();
1161:                    throw se;
1162:                } catch (URINotExistException se) {
1163:                    ctx.setRollbackOnly();
1164:                    throw se;
1165:                } catch (Exception e) {
1166:                    ctx.setRollbackOnly();
1167:                    throw new KernelServiceException("Error in chown : "
1168:                            + e.getMessage(), e);
1169:                }
1170:            }
1171:
1172:            /**
1173:             * @ejb.interface-method
1174:             * @ejb.transaction type="Supports"
1175:             */
1176:            public boolean checkIfCanChangeRights(URI user, URI uri)
1177:                    throws KernelServiceException,
1178:                    LibresourceSecurityException, URINotExistException {
1179:                try {
1180:                    return (checkIfOwner(uri) || (checkSecurity(uri,
1181:                            KernelConstants.SECURITY_CREATE)
1182:                            && checkSecurity(uri,
1183:                                    KernelConstants.SECURITY_DELETE)
1184:                            && checkSecurity(uri,
1185:                                    KernelConstants.SECURITY_UPDATE) && checkSecurity(
1186:                            uri, KernelConstants.SECURITY_READ)));
1187:                } catch (URINotExistException se) {
1188:                    return false;
1189:                } catch (Exception e) {
1190:                    ctx.setRollbackOnly();
1191:                    throw new KernelServiceException(
1192:                            "Error in checkIfCanChangeRights : "
1193:                                    + e.getMessage(), e);
1194:                }
1195:            }
1196:
1197:            /**
1198:             * @ejb.interface-method
1199:             * @ejb.transaction type="Supports"
1200:             */
1201:            public boolean checkIfCanChangeRights(URI uri)
1202:                    throws KernelServiceException,
1203:                    LibresourceSecurityException, URINotExistException {
1204:                return checkIfCanChangeRights(getConnectedResource(), uri);
1205:            }
1206:
1207:            /**
1208:             * @ejb.interface-method
1209:             * @ejb.transaction type="Required"
1210:             */
1211:            public void createAcl(URI forUri, URI resource, String[] permissions)
1212:                    throws KernelServiceException,
1213:                    LibresourceSecurityException, URINotExistException {
1214:                try {
1215:                    NodeLocal forUriNode;
1216:                    NodeLocal resourceNode;
1217:
1218:                    try {
1219:                        forUriNode = NodeUtil.getLocalHome().getByPath(
1220:                                normalizeAbsoluteURIPath(forUri));
1221:                    } catch (ObjectNotFoundException e) {
1222:                        throw new URINotExistException(normalizeURI(forUri));
1223:                    }
1224:
1225:                    try {
1226:                        resourceNode = NodeUtil.getLocalHome().getByPath(
1227:                                normalizeAbsoluteURIPath(resource));
1228:                    } catch (ObjectNotFoundException e) {
1229:                        throw new URINotExistException(normalizeURI(resource));
1230:                    }
1231:
1232:                    if (!checkIfCanChangeRights(forUri)) {
1233:                        throw new LibresourceSecurityException(forUri,
1234:                                "OWNER or CRUD");
1235:                    }
1236:
1237:                    HashMap acls = forUriNode.getAcls();
1238:
1239:                    if (acls == null) {
1240:                        acls = new HashMap();
1241:                    }
1242:
1243:                    // Keep only owner that have at least on right
1244:                    String[] resultingPermission = filterPermissions(
1245:                            LibresourceResourceIdentifier
1246:                                    .deserialize(forUriNode
1247:                                            .getBindedResourceIdentifier()),
1248:                            permissions);
1249:
1250:                    if (resultingPermission.length > 0) {
1251:                        acls.put(resourceNode.getURI(), resultingPermission);
1252:                    }
1253:
1254:                    forUriNode.setAcls(acls);
1255:                    forUriNode.setUpdateDate(new Date());
1256:
1257:                    // event
1258:                    Libresource.throwEvent(new LibresourceEvent(forUri,
1259:                            KernelConstants.SERVICE, "Node",
1260:                            getConnectedResource(),
1261:                            KernelConstants.EVENT_CREATE_ACLS));
1262:                } catch (LibresourceSecurityException se) {
1263:                    ctx.setRollbackOnly();
1264:                    throw se;
1265:                } catch (URINotExistException se) {
1266:                    ctx.setRollbackOnly();
1267:                    throw se;
1268:                } catch (Exception e) {
1269:                    ctx.setRollbackOnly();
1270:                    throw new KernelServiceException("Error in createAcl : "
1271:                            + e.getMessage(), e);
1272:                }
1273:            }
1274:
1275:            /**
1276:             * @ejb.interface-method
1277:             * @ejb.transaction type="Required"
1278:             */
1279:            public void createAclRecursively(URI forUri, URI resource,
1280:                    String[] permissions) throws KernelServiceException,
1281:                    LibresourceSecurityException, URINotExistException {
1282:                try {
1283:                    NodeLocal forUriNode;
1284:                    NodeLocal resourceNode;
1285:
1286:                    try {
1287:                        forUriNode = NodeUtil.getLocalHome().getByPath(
1288:                                normalizeAbsoluteURIPath(forUri));
1289:                    } catch (ObjectNotFoundException e) {
1290:                        throw new URINotExistException(normalizeURI(forUri));
1291:                    }
1292:
1293:                    try {
1294:                        resourceNode = NodeUtil.getLocalHome().getByPath(
1295:                                normalizeAbsoluteURIPath(resource));
1296:                    } catch (ObjectNotFoundException e) {
1297:                        throw new URINotExistException(normalizeURI(resource));
1298:                    }
1299:
1300:                    if (!checkIfCanChangeRights(forUri)) {
1301:                        throw new LibresourceSecurityException(forUri,
1302:                                "OWNER or CRUD");
1303:                    }
1304:
1305:                    // recursive
1306:                    Collection children = forUriNode.getChildren2();
1307:
1308:                    for (Iterator i = children.iterator(); i.hasNext();) {
1309:                        NodeLocal child = (NodeLocal) i.next();
1310:                        createAclRecursively(child.getURI(), resource,
1311:                                permissions);
1312:                    }
1313:
1314:                    HashMap acls = forUriNode.getAcls();
1315:
1316:                    if (acls == null) {
1317:                        acls = new HashMap();
1318:                    }
1319:
1320:                    String[] existingPermissions = (String[]) acls
1321:                            .get(resourceNode.getURI());
1322:
1323:                    // keep specific existing permissions
1324:                    if (existingPermissions != null) {
1325:                        Vector v = new Vector();
1326:
1327:                        for (int i = 0; i < permissions.length; i++) {
1328:                            v.add(permissions[i]);
1329:                        }
1330:
1331:                        for (int i = 0; i < existingPermissions.length; i++) {
1332:                            if (!existingPermissions[i]
1333:                                    .startsWith(KernelConstants.SERVICE)) {
1334:                                v.add(existingPermissions[i]);
1335:                            }
1336:                        }
1337:
1338:                        String[] newPermissions = new String[v.size()];
1339:
1340:                        for (int i = 0; i < newPermissions.length; i++) {
1341:                            newPermissions[i] = (String) v.get(i);
1342:                        }
1343:
1344:                        permissions = newPermissions;
1345:                    }
1346:
1347:                    acls.put(resourceNode.getURI(), filterPermissions(
1348:                            LibresourceResourceIdentifier
1349:                                    .deserialize(forUriNode
1350:                                            .getBindedResourceIdentifier()),
1351:                            permissions));
1352:                    forUriNode.setAcls(acls);
1353:                    forUriNode.setUpdateDate(new Date());
1354:
1355:                    // event
1356:                    Libresource.throwEvent(new LibresourceEvent(forUri,
1357:                            KernelConstants.SERVICE, "Node",
1358:                            getConnectedResource(),
1359:                            KernelConstants.EVENT_CREATE_ACLS, permissions
1360:                                    + " for " + resource));
1361:                } catch (LibresourceSecurityException se) {
1362:                    ctx.setRollbackOnly();
1363:                    throw se;
1364:                } catch (URINotExistException se) {
1365:                    ctx.setRollbackOnly();
1366:                    throw se;
1367:                } catch (Exception e) {
1368:                    ctx.setRollbackOnly();
1369:                    throw new KernelServiceException("Error in createAcl : "
1370:                            + e.getMessage(), e);
1371:                }
1372:            }
1373:
1374:            /**
1375:             * @ejb.interface-method
1376:             * @ejb.transaction type="Required"
1377:             */
1378:            public void deleteAcl(URI forUri, URI resource, boolean recursive)
1379:                    throws KernelServiceException,
1380:                    LibresourceSecurityException, URINotExistException {
1381:                try {
1382:                    if (!checkIfCanChangeRights(forUri)) {
1383:                        throw new LibresourceSecurityException(forUri,
1384:                                "OWNER or CRUD");
1385:                    }
1386:
1387:                    NodeLocal forUriNode;
1388:
1389:                    // NodeLocal resourceNode;
1390:                    try {
1391:                        forUriNode = NodeUtil.getLocalHome().getByPath(
1392:                                normalizeAbsoluteURIPath(forUri));
1393:                    } catch (ObjectNotFoundException e) {
1394:                        throw new URINotExistException(normalizeURI(forUri));
1395:                    }
1396:
1397:                    // comment (allow deletion of an obsolete acl)
1398:
1399:                    /*
1400:                     * try { resourceNode =
1401:                     * NodeUtil.getLocalHome().getByPath(normalizeAbsoluteURIPath(resource)); }
1402:                     * catch (ObjectNotFoundException e) { throw new
1403:                     * URINotExistException(normalizeURI(resource)); }
1404:                     */
1405:                    HashMap acls = forUriNode.getAcls();
1406:                    URI del = null;
1407:
1408:                    for (Iterator i = acls.keySet().iterator(); i.hasNext();) {
1409:                        URI key = (URI) i.next();
1410:
1411:                        if (key.getPath().equals(
1412:                                normalizeURI(resource).getPath())) {
1413:                            del = key;
1414:                        }
1415:                    }
1416:
1417:                    acls.remove(del);
1418:                    forUriNode.setAcls(acls);
1419:                    forUriNode.setUpdateDate(new Date());
1420:
1421:                    // recursive
1422:                    if (recursive) {
1423:                        Collection children = forUriNode.getChildren2();
1424:
1425:                        for (Iterator i = children.iterator(); i.hasNext();) {
1426:                            NodeLocal child = (NodeLocal) i.next();
1427:                            deleteAcl(child.getURI(), resource, true);
1428:                        }
1429:                    }
1430:
1431:                    // event
1432:                    Libresource.throwEvent(new LibresourceEvent(forUri,
1433:                            KernelConstants.SERVICE, "Node",
1434:                            getConnectedResource(),
1435:                            KernelConstants.EVENT_DELETE_ACLS));
1436:                } catch (LibresourceSecurityException se) {
1437:                    ctx.setRollbackOnly();
1438:                    throw se;
1439:                } catch (URINotExistException se) {
1440:                    ctx.setRollbackOnly();
1441:                    throw se;
1442:                } catch (Exception e) {
1443:                    ctx.setRollbackOnly();
1444:                    throw new KernelServiceException("Error in deleteAcl : "
1445:                            + e.getMessage(), e);
1446:                }
1447:            }
1448:
1449:            /**
1450:             * @ejb.interface-method
1451:             * @ejb.transaction type="Supports"
1452:             */
1453:            public AclValue[] getAcls(URI uri) throws KernelServiceException,
1454:                    LibresourceSecurityException, URINotExistException {
1455:                try {
1456:                    NodeLocal uriNode;
1457:
1458:                    try {
1459:                        uriNode = NodeUtil.getLocalHome().getByPath(
1460:                                normalizeAbsoluteURIPath(uri));
1461:                    } catch (ObjectNotFoundException e) {
1462:                        throw new URINotExistException(normalizeURI(uri));
1463:                    }
1464:
1465:                    HashMap acls = uriNode.getAcls();
1466:                    AclValue[] result = new AclValue[acls.size()];
1467:                    int k = 0;
1468:
1469:                    for (Iterator i = acls.keySet().iterator(); i.hasNext();) {
1470:                        URI key = (URI) i.next();
1471:                        result[k] = new AclValue(key, (String[]) acls.get(key));
1472:                        result[k].setOwnerUri(normalizeURI(result[k]
1473:                                .getOwnerUri()));
1474:                        k++;
1475:                    }
1476:
1477:                    return result;
1478:                } catch (LibresourceSecurityException se) {
1479:                    throw se;
1480:                } catch (URINotExistException se) {
1481:                    throw se;
1482:                } catch (Exception e) {
1483:                    throw new KernelServiceException("Error in getAcls : "
1484:                            + e.getMessage(), e);
1485:                }
1486:            }
1487:
1488:            /**
1489:             * @ejb.interface-method
1490:             * @ejb.transaction type="Supports"
1491:             */
1492:            public String[] listKernelPermissions() {
1493:                return new String[] { KernelConstants.SECURITY_READ,
1494:                        KernelConstants.SECURITY_CREATE,
1495:                        KernelConstants.SECURITY_UPDATE,
1496:                        KernelConstants.SECURITY_DELETE };
1497:            }
1498:
1499:            /**
1500:             * @ejb.interface-method
1501:             * @ejb.transaction type="Supports"
1502:             */
1503:            public String[] listKernelEvents() {
1504:                return new String[] { KernelConstants.EVENT_CREATE,
1505:                        KernelConstants.EVENT_BIND,
1506:                        KernelConstants.EVENT_UNBIND,
1507:                        KernelConstants.EVENT_MOVE_TO,
1508:                        KernelConstants.EVENT_MOVE_FROM,
1509:                        KernelConstants.EVENT_DELETE,
1510:                        KernelConstants.EVENT_CHOWN,
1511:                        KernelConstants.EVENT_CREATE_ACLS,
1512:                        KernelConstants.EVENT_RESET_ACLS,
1513:                        KernelConstants.EVENT_DELETE_ACLS,
1514:                        KernelConstants.EVENT_SET_PROPERTY };
1515:            }
1516:
1517:            /**
1518:             * @ejb.interface-method
1519:             * @ejb.transaction type="Supports"
1520:             */
1521:            public boolean checkIfOwner(URI user, URI uri)
1522:                    throws KernelServiceException,
1523:                    LibresourceSecurityException, URINotExistException {
1524:                try {
1525:                    String connectedUser = normalizeAbsoluteURIPath(user);
1526:
1527:                    try {
1528:                        NodeLocal uriNode = NodeUtil.getLocalHome().getByPath(
1529:                                normalizeAbsoluteURIPath(uri));
1530:                        LibresourceResourceIdentifier ownerResourceIdentifier;
1531:
1532:                        if (uriNode.getOwner().trim().length() == 0) {
1533:                            return false;
1534:
1535:                            // ownerResourceIdentifier = null;
1536:                        } else {
1537:                            URI ownerUri = NodeUtil.getLocalHome()
1538:                                    .findByPrimaryKey(uriNode.getOwner())
1539:                                    .getURI();
1540:                            ownerResourceIdentifier = systemLookup(normalizeAbsoluteURIPath(ownerUri));
1541:                        }
1542:
1543:                        LibresourceResourceIdentifier connectedResourceIdentifier = systemLookup(connectedUser);
1544:
1545:                        if (connectedResourceIdentifier == null) {
1546:                            return false;
1547:                        }
1548:
1549:                        if (ownerResourceIdentifier != null) {
1550:                            LibresourceService libresourceService = Libresource
1551:                                    .getService(connectedResourceIdentifier
1552:                                            .getService());
1553:                            LibresourceService libresourceService2 = Libresource
1554:                                    .getService(ownerResourceIdentifier
1555:                                            .getService());
1556:
1557:                            if (libresourceService.canMapResourceForSecurity(
1558:                                    new URI(connectedUser), NodeUtil
1559:                                            .getLocalHome().findByPrimaryKey(
1560:                                                    uriNode.getOwner())
1561:                                            .getURI())
1562:                                    || libresourceService2
1563:                                            .canMapResourceForSecurity(new URI(
1564:                                                    connectedUser), NodeUtil
1565:                                                    .getLocalHome()
1566:                                                    .findByPrimaryKey(
1567:                                                            uriNode.getOwner())
1568:                                                    .getURI())) {
1569:                                return true;
1570:                            }
1571:                        } else {
1572:                            LibresourceService libresourceService = Libresource
1573:                                    .getService(connectedResourceIdentifier
1574:                                            .getService());
1575:
1576:                            if (libresourceService.canMapResourceForSecurity(
1577:                                    new URI(connectedUser), NodeUtil
1578:                                            .getLocalHome().findByPrimaryKey(
1579:                                                    uriNode.getOwner())
1580:                                            .getURI())) {
1581:                                return true;
1582:                            }
1583:                        }
1584:
1585:                        return false;
1586:                    } catch (ObjectNotFoundException e) {
1587:                        return false;
1588:
1589:                        // throw new URINotExistException(normalizeURI(uri));
1590:                    }
1591:                } catch (LibresourceSecurityException se) {
1592:                    throw se;
1593:                } catch (URINotExistException se) {
1594:                    throw se;
1595:                } catch (Exception e) {
1596:                    throw new KernelServiceException("Error in checkIfOwner : "
1597:                            + e.getMessage(), e);
1598:                }
1599:            }
1600:
1601:            /**
1602:             * @ejb.interface-method
1603:             * @ejb.transaction type="Supports"
1604:             */
1605:            public boolean checkIfOwner(URI uri) throws KernelServiceException,
1606:                    LibresourceSecurityException, URINotExistException {
1607:                try {
1608:                    if (!exist(uri)) {
1609:                        throw new URINotExistException(normalizeURI(uri));
1610:                    }
1611:
1612:                    if (isSuperUser()) {
1613:                        return true;
1614:                    }
1615:
1616:                    return checkIfOwner(getConnectedResource(), uri);
1617:                } catch (LibresourceSecurityException se) {
1618:                    throw se;
1619:                } catch (URINotExistException se) {
1620:                    throw se;
1621:                } catch (Exception e) {
1622:                    throw new KernelServiceException("Error in checkIfOwner : "
1623:                            + e.getMessage(), e);
1624:                }
1625:            }
1626:
1627:            /**
1628:             * @ejb.interface-method
1629:             * @ejb.transaction type="Supports"
1630:             */
1631:            public URI getConnectedResource() throws KernelServiceException,
1632:                    LibresourceSecurityException {
1633:                try {
1634:                    if (principals.get((isAuthentified()) ? ctx
1635:                            .getCallerPrincipal().getName() : ANONYMOUS) != null) {
1636:                        URI principal = normalizeURI((URI) (principals
1637:                                .get((isAuthentified()) ? ctx
1638:                                        .getCallerPrincipal().getName()
1639:                                        : ANONYMOUS)));
1640:
1641:                        return principal;
1642:                    }
1643:
1644:                    if (principalMapper == null) {
1645:                        StringTokenizer tokenizer = new StringTokenizer(
1646:                                (String) new InitialContext()
1647:                                        .lookup("java:comp/env/principal-mapper-options"),
1648:                                ";");
1649:                        Hashtable mapperOptions = new Hashtable();
1650:
1651:                        while (tokenizer.hasMoreTokens()) {
1652:                            String option = tokenizer.nextToken();
1653:                            mapperOptions.put(option.substring(0,
1654:                                    option.indexOf("=")).trim(), option
1655:                                    .substring(option.indexOf("=") + 1).trim());
1656:                        }
1657:
1658:                        principalMapper = (PrincipalMapper) Class
1659:                                .forName(
1660:                                        (String) new InitialContext()
1661:                                                .lookup("java:comp/env/principal-mapper"))
1662:                                .getConstructor(new Class[] { Map.class })
1663:                                .newInstance(new Object[] { mapperOptions });
1664:                    }
1665:
1666:                    URI resource = principalMapper.map((isAuthentified()) ? ctx
1667:                            .getCallerPrincipal() : null);
1668:                    principals.put((isAuthentified()) ? ctx
1669:                            .getCallerPrincipal().getName() : ANONYMOUS,
1670:                            resource);
1671:
1672:                    return normalizeURI(resource);
1673:                } catch (Exception e) {
1674:                    throw new KernelServiceException(
1675:                            "Error in getConnectedResource : " + e.getMessage(),
1676:                            e);
1677:                }
1678:            }
1679:
1680:            /**
1681:             * @ejb.interface-method
1682:             * @ejb.transaction type="Supports"
1683:             */
1684:            public boolean isAuthentified() throws KernelServiceException,
1685:                    LibresourceSecurityException {
1686:                try {
1687:                    return !(ctx.getCallerPrincipal().getName().equals(
1688:                            "client-login")
1689:                            || ctx.getCallerPrincipal().getName().equals(
1690:                                    "JOnAS_client")
1691:                            || ctx.getCallerPrincipal().getName().equals(
1692:                                    "jonas") || ctx.getCallerPrincipal()
1693:                            .getName().equals("ANONYMOUS"));
1694:                } catch (IllegalStateException e) {
1695:                    // no securityContext
1696:                    return false;
1697:                }
1698:            }
1699:
1700:            /**
1701:             * @ejb.interface-method
1702:             * @ejb.transaction type="Supports"
1703:             */
1704:            public boolean isSuperUser() throws KernelServiceException {
1705:                try {
1706:                    return ctx.getCallerPrincipal().getName().equals("root");
1707:                } catch (IllegalStateException e) {
1708:                    // no securityContext
1709:                    return false;
1710:                }
1711:            }
1712:
1713:            /**
1714:             * @ejb.interface-method
1715:             * @ejb.transaction type="Supports"
1716:             */
1717:            public int countResources(String service, String resourceType)
1718:                    throws KernelServiceException,
1719:                    LibresourceSecurityException, URINotExistException {
1720:                try {
1721:                    return NodeUtil.getLocalHome().countResource(service,
1722:                            resourceType);
1723:                } catch (Exception e) {
1724:                    throw new KernelServiceException(
1725:                            "Error in countResource : " + e.getMessage(), e);
1726:                }
1727:            }
1728:
1729:            /**
1730:             * @ejb.interface-method
1731:             * @ejb.transaction type="Supports"
1732:             */
1733:            public LibresourceResourceValue[] listResourcesAt(URI atUri,
1734:                    String service, String resourceType)
1735:                    throws KernelServiceException,
1736:                    LibresourceSecurityException, URINotExistException {
1737:                try {
1738:                    NodeLocal pathNodeLocal;
1739:
1740:                    try {
1741:                        pathNodeLocal = NodeUtil.getLocalHome().getByPath(
1742:                                normalizeAbsoluteURIPath(atUri));
1743:                    } catch (ObjectNotFoundException e) {
1744:                        throw new URINotExistException(normalizeURI(atUri));
1745:                    }
1746:
1747:                    if (!checkSecurity(atUri, KernelConstants.SECURITY_READ)) {
1748:                        return new LibresourceResourceValue[0];
1749:                    }
1750:
1751:                    Collection collection = NodeUtil.getLocalHome()
1752:                            .findResources(pathNodeLocal.getId(),
1753:                                    service + "/" + resourceType + "/%");
1754:                    Vector filteredFor_R_RightUris = new Vector();
1755:
1756:                    for (Iterator i = collection.iterator(); i.hasNext();) {
1757:                        NodeLocal uriNode = (NodeLocal) i.next();
1758:
1759:                        if (checkSecurity(uriNode.getURI(),
1760:                                KernelConstants.SECURITY_READ)) {
1761:                            LibresourceResourceValue libresourceResourceValue = new LibresourceResourceValue();
1762:                            libresourceResourceValue
1763:                                    .setUri(normalizeURI(uriNode.getURI()));
1764:
1765:                            LibresourceResourceIdentifier resourceIdentifier = LibresourceResourceIdentifier
1766:                                    .deserialize(uriNode
1767:                                            .getBindedResourceIdentifier());
1768:                            libresourceResourceValue
1769:                                    .setShortResourceName(getShortName(normalizeURI(uriNode
1770:                                            .getURI()))); // resourceIdentifier));
1771:                            libresourceResourceValue.setCreationDate(uriNode
1772:                                    .getCreationDate());
1773:                            libresourceResourceValue.setUpdateDate(uriNode
1774:                                    .getUpdateDate());
1775:
1776:                            try {
1777:                                libresourceResourceValue
1778:                                        .setOwner(getOwner(uriNode.getURI()));
1779:                            } catch (LibresourceSecurityException e) {
1780:                                libresourceResourceValue.setOwner(null);
1781:                            }
1782:
1783:                            libresourceResourceValue
1784:                                    .setLibresourceResourceIdentifier(resourceIdentifier);
1785:                            filteredFor_R_RightUris
1786:                                    .add(libresourceResourceValue);
1787:                        }
1788:                    }
1789:
1790:                    // sort the list by ShortResourceName
1791:                    filteredFor_R_RightUris = sortResourcesList(filteredFor_R_RightUris);
1792:
1793:                    LibresourceResourceValue[] resourceValues = new LibresourceResourceValue[filteredFor_R_RightUris
1794:                            .size()];
1795:
1796:                    for (int i = 0; i < resourceValues.length; i++) {
1797:                        resourceValues[i] = (LibresourceResourceValue) filteredFor_R_RightUris
1798:                                .get(i);
1799:                    }
1800:
1801:                    return resourceValues;
1802:                } catch (URINotExistException se) {
1803:                    throw se;
1804:                } catch (Exception e) {
1805:                    throw new KernelServiceException(
1806:                            "Error in listResourcesAt : " + e.getMessage(), e);
1807:                }
1808:            }
1809:
1810:            /**
1811:             * @ejb.interface-method
1812:             * @ejb.transaction type="Supports"
1813:             */
1814:            public LibresourceResourceValue[] listResourcesAt(URI atUri)
1815:                    throws KernelServiceException,
1816:                    LibresourceSecurityException, URINotExistException {
1817:                try {
1818:                    NodeLocal pathNodeLocal;
1819:
1820:                    try {
1821:                        pathNodeLocal = NodeUtil.getLocalHome().getByPath(
1822:                                normalizeAbsoluteURIPath(atUri));
1823:                    } catch (ObjectNotFoundException e) {
1824:                        throw new URINotExistException(normalizeURI(atUri));
1825:                    }
1826:
1827:                    if (!checkSecurity(atUri, KernelConstants.SECURITY_READ)) {
1828:                        return new LibresourceResourceValue[0];
1829:                    }
1830:
1831:                    Collection collection = pathNodeLocal.getChildren2();
1832:                    Vector filteredFor_R_RightUris = new Vector();
1833:                    Vector resources = new Vector();
1834:                    Vector empty = new Vector();
1835:
1836:                    for (Iterator i = collection.iterator(); i.hasNext();) {
1837:                        NodeLocal uriNode = (NodeLocal) i.next();
1838:
1839:                        if (checkSecurity(uriNode.getURI(),
1840:                                KernelConstants.SECURITY_READ)) {
1841:                            LibresourceResourceValue libresourceResourceValue = new LibresourceResourceValue();
1842:                            libresourceResourceValue
1843:                                    .setUri(normalizeURI(uriNode.getURI()));
1844:
1845:                            if (uriNode.getBindedResourceIdentifier() != null) {
1846:                                LibresourceResourceIdentifier resourceIdentifier = LibresourceResourceIdentifier
1847:                                        .deserialize(uriNode
1848:                                                .getBindedResourceIdentifier());
1849:                                libresourceResourceValue
1850:                                        .setShortResourceName(getShortName(normalizeURI(uriNode
1851:                                                .getURI()))); // resourceIdentifier));
1852:                                libresourceResourceValue
1853:                                        .setLibresourceResourceIdentifier(resourceIdentifier);
1854:                                libresourceResourceValue
1855:                                        .setCreationDate(uriNode
1856:                                                .getCreationDate());
1857:                                libresourceResourceValue.setUpdateDate(uriNode
1858:                                        .getUpdateDate());
1859:
1860:                                try {
1861:                                    libresourceResourceValue
1862:                                            .setOwner(getOwner(uriNode.getURI()));
1863:                                } catch (LibresourceSecurityException e) {
1864:                                    libresourceResourceValue.setOwner(null);
1865:                                }
1866:
1867:                                resources.add(libresourceResourceValue);
1868:                            } else {
1869:                                libresourceResourceValue.setIsEmpty(true);
1870:                                libresourceResourceValue
1871:                                        .setShortResourceName("/"
1872:                                                + uriNode.getPathPart()
1873:                                                + " (Empty Node)");
1874:                                libresourceResourceValue
1875:                                        .setCreationDate(uriNode
1876:                                                .getCreationDate());
1877:                                libresourceResourceValue.setUpdateDate(uriNode
1878:                                        .getUpdateDate());
1879:
1880:                                try {
1881:                                    libresourceResourceValue
1882:                                            .setOwner(getOwner(uriNode.getURI()));
1883:                                } catch (LibresourceSecurityException e) {
1884:                                    libresourceResourceValue.setOwner(null);
1885:                                }
1886:
1887:                                empty.add(libresourceResourceValue);
1888:                            }
1889:                        }
1890:                    }
1891:
1892:                    // sort the list by ShortResourceName
1893:                    filteredFor_R_RightUris = sortResourcesList(resources);
1894:                    filteredFor_R_RightUris.addAll(empty);
1895:
1896:                    LibresourceResourceValue[] resourceValues = new LibresourceResourceValue[filteredFor_R_RightUris
1897:                            .size()];
1898:
1899:                    for (int i = 0; i < resourceValues.length; i++) {
1900:                        resourceValues[i] = (LibresourceResourceValue) filteredFor_R_RightUris
1901:                                .get(i);
1902:                    }
1903:
1904:                    return resourceValues;
1905:                } catch (URINotExistException se) {
1906:                    throw se;
1907:                } catch (Exception e) {
1908:                    e.printStackTrace();
1909:                    throw new KernelServiceException(
1910:                            "Error in listResourcesAt : " + e.getMessage(), e);
1911:                }
1912:            }
1913:
1914:            /**
1915:             * @ejb.interface-method
1916:             * @ejb.transaction type="Supports"
1917:             */
1918:            public LibresourceResourceValue getResource(URI atUri)
1919:                    throws KernelServiceException,
1920:                    LibresourceSecurityException, URINotExistException {
1921:                try {
1922:                    LibresourceResourceValue libresourceResourceValue = new LibresourceResourceValue();
1923:                    libresourceResourceValue.setUri(normalizeURI(atUri));
1924:
1925:                    LibresourceResourceIdentifier resourceIdentifier = systemLookup(atUri);
1926:
1927:                    if (resourceIdentifier != null) {
1928:                        libresourceResourceValue
1929:                                .setShortResourceName(getShortName(normalizeURI(atUri))); // resourceIdentifier));
1930:                        libresourceResourceValue
1931:                                .setLibresourceResourceIdentifier(resourceIdentifier);
1932:                    } else {
1933:                        libresourceResourceValue.setIsEmpty(true);
1934:                        libresourceResourceValue
1935:                                .setShortResourceName(libresourceResourceValue
1936:                                        .getUri().toString().substring(
1937:                                                libresourceResourceValue
1938:                                                        .getUri().toString()
1939:                                                        .lastIndexOf("/")));
1940:                    }
1941:
1942:                    NodeLocal node = NodeUtil.getLocalHome().getByPath(
1943:                            normalizeAbsoluteURIPath(atUri));
1944:                    libresourceResourceValue.setCreationDate(node
1945:                            .getCreationDate());
1946:                    libresourceResourceValue
1947:                            .setUpdateDate(node.getUpdateDate());
1948:                    libresourceResourceValue.setOwner(getOwner(node.getURI()));
1949:
1950:                    return libresourceResourceValue;
1951:                } catch (URINotExistException se) {
1952:                    throw se;
1953:                } catch (LibresourceSecurityException se) {
1954:                    throw se;
1955:                } catch (Exception e) {
1956:                    throw new KernelServiceException("Error in getResource : "
1957:                            + e.getMessage(), e);
1958:                }
1959:            }
1960:
1961:            /**
1962:             * @ejb.interface-method
1963:             * @ejb.transaction type="Supports"
1964:             */
1965:            public Vector getNavigationBar(URI atUri)
1966:                    throws KernelServiceException,
1967:                    LibresourceSecurityException, URINotExistException {
1968:                try {
1969:                    NodeLocal pathNodeLocal;
1970:
1971:                    try {
1972:                        pathNodeLocal = NodeUtil.getLocalHome().getByPath(
1973:                                normalizeAbsoluteURIPath(atUri));
1974:                    } catch (ObjectNotFoundException e) {
1975:                        throw new URINotExistException(normalizeURI(atUri));
1976:                    }
1977:
1978:                    Vector bar = new Vector();
1979:
1980:                    while (pathNodeLocal != null) {
1981:                        String[] node = new String[3];
1982:                        node[0] = normalizeURI(pathNodeLocal.getURI())
1983:                                .getPath();
1984:                        node[1] = pathNodeLocal.getPathPart();
1985:
1986:                        if (pathNodeLocal.getBindedResourceIdentifier() != null) {
1987:                            LibresourceResourceIdentifier resourceIdentifier = LibresourceResourceIdentifier
1988:                                    .deserialize(pathNodeLocal
1989:                                            .getBindedResourceIdentifier());
1990:                            node[2] = Libresource
1991:                                    .getShortName(normalizeURI(pathNodeLocal
1992:                                            .getURI())); // resourceIdentifier);
1993:                        } else {
1994:                            node[2] = pathNodeLocal.getPathPart();
1995:                        }
1996:
1997:                        bar.add(node);
1998:
1999:                        if (pathNodeLocal.getParent().trim().length() == 0) {
2000:                            pathNodeLocal = null;
2001:                        } else {
2002:                            pathNodeLocal = NodeUtil
2003:                                    .getLocalHome()
2004:                                    .findByPrimaryKey(pathNodeLocal.getParent());
2005:                        }
2006:                    }
2007:
2008:                    Collections.reverse(bar);
2009:
2010:                    return bar;
2011:                } catch (URINotExistException se) {
2012:                    throw se;
2013:                } catch (Exception e) {
2014:                    throw new KernelServiceException(
2015:                            "Error in getNavigationBar : " + e.getMessage(), e);
2016:                }
2017:            }
2018:
2019:            /**
2020:             * @ejb.interface-method
2021:             * @ejb.transaction type="Supports"
2022:             */
2023:            public URI[] listChildren(URI atUri) throws KernelServiceException,
2024:                    LibresourceSecurityException, URINotExistException {
2025:                try {
2026:                    NodeLocal pathNodeLocal;
2027:
2028:                    try {
2029:                        pathNodeLocal = NodeUtil.getLocalHome().getByPath(
2030:                                normalizeAbsoluteURIPath(atUri));
2031:                    } catch (ObjectNotFoundException e) {
2032:                        throw new URINotExistException(normalizeURI(atUri));
2033:                    }
2034:
2035:                    if (!checkSecurity(atUri, KernelConstants.SECURITY_READ)) {
2036:                        return new URI[0];
2037:                    }
2038:
2039:                    Collection collection = pathNodeLocal.getChildren2();
2040:                    Vector filteredFor_R_RightUris = new Vector();
2041:
2042:                    for (Iterator i = collection.iterator(); i.hasNext();) {
2043:                        URI uri = ((NodeLocal) i.next()).getURI();
2044:
2045:                        // if (checkSecurity(uri, "Kernel:READ")) {
2046:                        filteredFor_R_RightUris.add(uri);
2047:
2048:                        // }
2049:                    }
2050:
2051:                    // sort the uris list
2052:                    filteredFor_R_RightUris = sortUrisList(filteredFor_R_RightUris);
2053:
2054:                    URI[] uris = new URI[filteredFor_R_RightUris.size()];
2055:
2056:                    for (int i = 0; i < uris.length; i++) {
2057:                        uris[i] = (URI) filteredFor_R_RightUris.get(i);
2058:                    }
2059:
2060:                    return uris;
2061:                } catch (URINotExistException se) {
2062:                    throw se;
2063:                } catch (Exception e) {
2064:                    throw new KernelServiceException("Error in listChildren : "
2065:                            + e.getMessage(), e);
2066:                }
2067:            }
2068:
2069:            /**
2070:             * @ejb.interface-method
2071:             * @ejb.transaction type="Supports"
2072:             */
2073:            public boolean hasChildren(URI atUri)
2074:                    throws KernelServiceException,
2075:                    LibresourceSecurityException, URINotExistException {
2076:                try {
2077:                    NodeLocal pathNodeLocal;
2078:
2079:                    try {
2080:                        pathNodeLocal = NodeUtil.getLocalHome().getByPath(
2081:                                normalizeAbsoluteURIPath(atUri));
2082:                    } catch (ObjectNotFoundException e) {
2083:                        throw new URINotExistException(normalizeURI(atUri));
2084:                    }
2085:
2086:                    return pathNodeLocal.getChildren().isEmpty();
2087:                } catch (URINotExistException se) {
2088:                    throw se;
2089:                } catch (Exception e) {
2090:                    throw new KernelServiceException("Error in hasChildren : "
2091:                            + e.getMessage(), e);
2092:                }
2093:            }
2094:
2095:            /**
2096:             * @ejb.interface-method
2097:             * @ejb.transaction type="Supports"
2098:             */
2099:            public boolean checkSecurity(URI user, URI uri, String permission)
2100:                    throws KernelServiceException,
2101:                    LibresourceSecurityException, URINotExistException {
2102:                try {
2103:                    String connectedResource = normalizeAbsoluteURIPath(user);
2104:
2105:                    // owner has all permissions
2106:                    if (checkIfOwner(user, uri)) {
2107:                        return true;
2108:                    }
2109:
2110:                    /*
2111:                     * if (permission.equals(KernelConstants.SECURITY_KERNEL_READ)) { if
2112:                     * (connectedResource.equals(normalizeAbsoluteURIPath(uri))) {
2113:                     * return true; } }
2114:                     */
2115:
2116:                    // Check before the Kernel:Update right if its a specific right
2117:                    if (!permission.startsWith(KernelConstants.SECURITY)
2118:                            && checkSecurity(user, uri,
2119:                                    KernelConstants.SECURITY_UPDATE)) {
2120:                        return true;
2121:                    }
2122:
2123:                    try {
2124:                        NodeLocal uriNode = NodeUtil.getLocalHome().getByPath(
2125:                                normalizeAbsoluteURIPath(uri));
2126:                        HashMap acls = uriNode.getAcls();
2127:
2128:                        for (Iterator i = acls.keySet().iterator(); i.hasNext();) {
2129:                            URI key = (URI) i.next();
2130:
2131:                            // TODO remove obsolete acls...
2132:                            if (exist(key)) {
2133:                                LibresourceResourceIdentifier ownerResourceIdentifier = systemLookup(normalizeAbsoluteURIPath(key));
2134:                                LibresourceResourceIdentifier connectedResourceIdentifier = systemLookup(connectedResource);
2135:
2136:                                if (connectedResourceIdentifier == null) {
2137:                                    return false;
2138:                                }
2139:
2140:                                LibresourceService libresourceService = Libresource
2141:                                        .getService(connectedResourceIdentifier
2142:                                                .getService());
2143:                                LibresourceService libresourceService2 = Libresource
2144:                                        .getService(ownerResourceIdentifier
2145:                                                .getService());
2146:
2147:                                if (libresourceService
2148:                                        .canMapResourceForSecurity(new URI(
2149:                                                connectedResource), new URI(
2150:                                                normalizeAbsoluteURIPath(key)))
2151:                                        || libresourceService2
2152:                                                .canMapResourceForSecurity(
2153:                                                        new URI(
2154:                                                                connectedResource),
2155:                                                        new URI(
2156:                                                                normalizeAbsoluteURIPath(key)))) {
2157:                                    if (contains((String[]) acls.get(key),
2158:                                            permission)) {
2159:                                        return true;
2160:                                    }
2161:                                }
2162:                            }
2163:                        }
2164:
2165:                        return false;
2166:                    } catch (ObjectNotFoundException e) {
2167:                        return false;
2168:
2169:                        // throw new URINotExistException(normalizeURI(uri));
2170:                    }
2171:                } catch (URINotExistException se) {
2172:                    throw se;
2173:                } catch (LibresourceSecurityException se) {
2174:                    throw se;
2175:                } catch (Exception e) {
2176:                    throw new KernelServiceException(
2177:                            "Error in checkSecurity : " + e.getMessage(), e);
2178:                }
2179:            }
2180:
2181:            /**
2182:             * @ejb.interface-method
2183:             * @ejb.transaction type="Supports"
2184:             */
2185:            public boolean checkSecurity(URI uri, String permission)
2186:                    throws KernelServiceException,
2187:                    LibresourceSecurityException, URINotExistException {
2188:                try {
2189:                    if (!exist(uri)) {
2190:                        throw new URINotExistException(normalizeURI(uri));
2191:                    }
2192:
2193:                    if (isSuperUser()) {
2194:                        return true;
2195:                    }
2196:
2197:                    return checkSecurity(getConnectedResource(), uri,
2198:                            permission);
2199:                } catch (URINotExistException se) {
2200:                    return false;
2201:                } catch (LibresourceSecurityException se) {
2202:                    throw se;
2203:                } catch (Exception e) {
2204:                    throw new KernelServiceException(
2205:                            "Error in checkSecurity : " + e.getMessage(), e);
2206:                }
2207:            }
2208:
2209:            /**
2210:             * @ejb.interface-method
2211:             * @ejb.transaction type="Supports"
2212:             */
2213:            public boolean zubZum(URI connectedUri, URI toUri)
2214:                    throws KernelServiceException,
2215:                    LibresourceSecurityException, URINotExistException {
2216:                try {
2217:                    if (isSuperUser()) {
2218:                        return true;
2219:                    }
2220:
2221:                    LibresourceResourceIdentifier connectedResource = systemLookup(normalizeAbsoluteURIPath(connectedUri));
2222:                    LibresourceResourceIdentifier toResource = null;
2223:
2224:                    try {
2225:                        toResource = systemLookup(normalizeAbsoluteURIPath(toUri));
2226:                    } catch (Exception e) {
2227:                        return false;
2228:                    }
2229:
2230:                    LibresourceService connectedResourceService = Libresource
2231:                            .getService(connectedResource.getService());
2232:                    LibresourceService toResourceService = Libresource
2233:                            .getService(toResource.getService());
2234:
2235:                    return connectedResourceService.canMapResourceForSecurity(
2236:                            new URI(normalizeAbsoluteURIPath(connectedUri)),
2237:                            new URI(normalizeAbsoluteURIPath(toUri)))
2238:                            || toResourceService
2239:                                    .canMapResourceForSecurity(
2240:                                            new URI(
2241:                                                    normalizeAbsoluteURIPath(connectedUri)),
2242:                                            new URI(
2243:                                                    normalizeAbsoluteURIPath(toUri)));
2244:                } catch (URINotExistException se) {
2245:                    throw se;
2246:                } catch (LibresourceSecurityException se) {
2247:                    throw se;
2248:                } catch (Exception e) {
2249:                    throw new KernelServiceException("Error in zubZum : "
2250:                            + e.getMessage(), e);
2251:                }
2252:            }
2253:
2254:            /**
2255:             * @ejb.interface-method
2256:             * @ejb.transaction type="Supports"
2257:             */
2258:            public boolean zubZum(URI toUri) throws KernelServiceException,
2259:                    LibresourceSecurityException, URINotExistException {
2260:                return zubZum(getConnectedResource(), toUri);
2261:            }
2262:
2263:            /**
2264:             * Seb coding to prevent stack overflow
2265:             * @ejb.interface-method
2266:             * @ejb.transaction type="Supports"
2267:             */
2268:            public void indexAll() throws KernelServiceException,
2269:                    LibresourceSecurityException, URINotExistException {
2270:                try {
2271:                    Collection nodes = NodeUtil.getLocalHome().findAll();
2272:                    NodeLocal currentNode = null;
2273:
2274:                    for (Iterator i = nodes.iterator(); i.hasNext();) {
2275:                        currentNode = (NodeLocal) i.next();
2276:
2277:                        if (currentNode.hasResourceBinded()) {
2278:                            System.out.println("Indexing: "
2279:                                    + currentNode.getURI());
2280:
2281:                            try {
2282:                                Libresource.index(currentNode.getURI());
2283:                                System.out.println(" + OK");
2284:                            } catch (RuntimeException e) {
2285:                                System.out.println(" - KO");
2286:                            }
2287:                        } else {
2288:                            System.out.println("NOT Indexing: "
2289:                                    + currentNode.getURI());
2290:                        }
2291:                    }
2292:                } catch (Exception e) {
2293:                    throw new KernelServiceException("Error in indexAll : "
2294:                            + e.getMessage(), e);
2295:                }
2296:            }
2297:
2298:            /**
2299:             * @ejb.interface-method
2300:             * @ejb.transaction type="Supports"
2301:             */
2302:            public void flushIndexBase() throws KernelServiceException,
2303:                    LibresourceSecurityException, URINotExistException {
2304:                try {
2305:                    LibresourceSearch.getInstance().flush();
2306:                } catch (Exception e) {
2307:                    throw new KernelServiceException(
2308:                            "Error in flushIndexBase : " + e.getMessage(), e);
2309:                }
2310:            }
2311:
2312:            /**
2313:             * @ejb.interface-method
2314:             * @ejb.transaction type="Supports"
2315:             */
2316:            public LibresourceSearchResult[] search(String query)
2317:                    throws KernelServiceException, LibresourceSecurityException {
2318:                return search(null, null, null, query);
2319:            }
2320:
2321:            /**
2322:             * @ejb.interface-method
2323:             * @ejb.transaction type="Supports"
2324:             */
2325:            public String highlight(String text, String quey)
2326:                    throws KernelServiceException, LibresourceSecurityException {
2327:                try {
2328:                    return LibresourceSearch.getInstance()
2329:                            .highlight(text, quey);
2330:                } catch (LibresourceException e) {
2331:                    throw new KernelServiceException(e);
2332:                }
2333:            }
2334:
2335:            /**
2336:             * @ejb.interface-method
2337:             * @ejb.transaction type="Supports"
2338:             */
2339:            public LibresourceSearchResult[] search(String uriPattern,
2340:                    String service, String resource, String query)
2341:                    throws KernelServiceException, LibresourceSecurityException {
2342:                try {
2343:                    LibresourceSearchResult[] results = LibresourceSearch
2344:                            .getInstance().search(uriPattern, service,
2345:                                    resource, query);
2346:                    Vector filteredResults = new Vector();
2347:
2348:                    for (int i = 0; i < results.length; i++) {
2349:                        try {
2350:                            if (checkSecurity(results[i].getUri(),
2351:                                    KernelConstants.SECURITY_READ)) {
2352:                                filteredResults.add(results[i]);
2353:                            }
2354:                        } catch (Exception e) {
2355:                            //
2356:                        }
2357:                    }
2358:
2359:                    LibresourceSearchResult[] finalResults = new LibresourceSearchResult[filteredResults
2360:                            .size()];
2361:
2362:                    for (int i = 0; i < filteredResults.size(); i++) {
2363:                        finalResults[i] = (LibresourceSearchResult) filteredResults
2364:                                .get(i);
2365:                        finalResults[i]
2366:                                .setResourceIdentifier(systemLookup(normalizeAbsoluteURIPath(finalResults[i]
2367:                                        .getUri())));
2368:                    }
2369:
2370:                    return finalResults;
2371:                } catch (Exception e) {
2372:                    throw new KernelServiceException("Error in search(" + query
2373:                            + ") : " + e.getMessage(), e);
2374:                }
2375:            }
2376:
2377:            /**
2378:             * @ejb.interface-method
2379:             * @ejb.transaction type="Supports"
2380:             */
2381:            public Object getPropertyObject(URI uri, String key)
2382:                    throws KernelServiceException,
2383:                    LibresourceSecurityException, URINotExistException {
2384:                return getPropertyObject(uri, key, false);
2385:            }
2386:
2387:            /**
2388:             * @ejb.interface-method
2389:             * @ejb.transaction type="Supports"
2390:             */
2391:            public String getProperty(URI uri, String key)
2392:                    throws KernelServiceException,
2393:                    LibresourceSecurityException, URINotExistException {
2394:                return getProperty(uri, key, false);
2395:            }
2396:
2397:            /**
2398:             * @ejb.interface-method
2399:             * @ejb.transaction type="Supports"
2400:             */
2401:            public URI getNodeDefiningProperty(URI uri, String key)
2402:                    throws KernelServiceException,
2403:                    LibresourceSecurityException, URINotExistException {
2404:                try {
2405:                    NodeLocal node = null;
2406:
2407:                    try {
2408:                        node = NodeUtil.getLocalHome().getByPath(
2409:                                normalizeAbsoluteURIPath(uri));
2410:                    } catch (ObjectNotFoundException e) {
2411:                        throw new URINotExistException(normalizeURI(uri));
2412:                    }
2413:
2414:                    while (node != null) {
2415:                        Object result = getPropertyObject(node, key);
2416:
2417:                        if (result != null) {
2418:                            return normalizeURI(node.getURI());
2419:                        }
2420:
2421:                        if ((node.getParent() == null)
2422:                                || (node.getParent().trim().length() == 0)) {
2423:                            node = null;
2424:                        } else {
2425:                            node = NodeUtil.getLocalHome().findByPrimaryKey(
2426:                                    node.getParent());
2427:                        }
2428:                    }
2429:
2430:                    return null;
2431:                } catch (URINotExistException se) {
2432:                    throw se;
2433:                } catch (LibresourceSecurityException se) {
2434:                    throw se;
2435:                } catch (Exception e) {
2436:                    throw new KernelServiceException(
2437:                            "Error in getNodeDefiningProperty : "
2438:                                    + e.getMessage(), e);
2439:                }
2440:            }
2441:
2442:            /**
2443:             * @ejb.interface-method
2444:             * @ejb.transaction type="Supports"
2445:             */
2446:            public String getProperty(URI uri, String key, boolean inherit)
2447:                    throws KernelServiceException,
2448:                    LibresourceSecurityException, URINotExistException {
2449:                return (String) getPropertyObject(uri, key, inherit);
2450:            }
2451:
2452:            /**
2453:             * @ejb.interface-method
2454:             * @ejb.transaction type="Supports"
2455:             */
2456:            public Object getPropertyObject(URI uri, String key, boolean inherit)
2457:                    throws KernelServiceException,
2458:                    LibresourceSecurityException, URINotExistException {
2459:                try {
2460:                    if (!exist(uri)) {
2461:                        return null;
2462:                    }
2463:
2464:                    if (!checkSecurity(uri, KernelConstants.SECURITY_READ)) {
2465:                        return null;
2466:                    }
2467:
2468:                    try {
2469:                        NodeLocal node = NodeUtil.getLocalHome().getByPath(
2470:                                normalizeAbsoluteURIPath(uri));
2471:
2472:                        if (!inherit) {
2473:                            return getPropertyObject(node, key);
2474:                        } else {
2475:                            URI parent = getNodeDefiningProperty(uri, key);
2476:
2477:                            if (parent != null) {
2478:                                return getPropertyObject(parent, key, false);
2479:                            }
2480:                        }
2481:
2482:                        return null;
2483:                    } catch (ObjectNotFoundException e) {
2484:                        throw new URINotExistException(normalizeURI(uri));
2485:                    }
2486:                } catch (URINotExistException se) {
2487:                    throw se;
2488:                } catch (LibresourceSecurityException se) {
2489:                    throw se;
2490:                } catch (Exception e) {
2491:                    throw new KernelServiceException("Error in getProperty : "
2492:                            + e.getMessage(), e);
2493:                }
2494:            }
2495:
2496:            /**
2497:             * @ejb.interface-method
2498:             * @ejb.transaction type="Supports"
2499:             */
2500:            public HashMap listPropertiesAtNode(URI uri)
2501:                    throws KernelServiceException,
2502:                    LibresourceSecurityException, URINotExistException {
2503:                try {
2504:                    if (!checkSecurity(uri, KernelConstants.SECURITY_READ)) {
2505:                        throw new LibresourceSecurityException(uri,
2506:                                KernelConstants.SECURITY_READ);
2507:                    }
2508:
2509:                    try {
2510:                        NodeLocal node = NodeUtil.getLocalHome().getByPath(
2511:                                normalizeAbsoluteURIPath(uri));
2512:
2513:                        HashMap map = node.getProperties();
2514:
2515:                        if (map == null) {
2516:                            return new HashMap();
2517:                        }
2518:
2519:                        return map;
2520:                    } catch (ObjectNotFoundException e) {
2521:                        throw new URINotExistException(normalizeURI(uri));
2522:                    }
2523:                } catch (URINotExistException se) {
2524:                    throw se;
2525:                } catch (LibresourceSecurityException se) {
2526:                    throw se;
2527:                } catch (Exception e) {
2528:                    throw new KernelServiceException(
2529:                            "Error in listPropertiesAtNode : " + e.getMessage(),
2530:                            e);
2531:                }
2532:            }
2533:
2534:            /**
2535:             * @ejb.interface-method
2536:             * @ejb.transaction type="Required"
2537:             */
2538:            public void setProperty(URI uri, String key, Object value)
2539:                    throws KernelServiceException,
2540:                    LibresourceSecurityException, URINotExistException {
2541:                try {
2542:                    if (!checkSecurity(uri, KernelConstants.SECURITY_UPDATE)) {
2543:                        throw new LibresourceSecurityException(uri,
2544:                                KernelConstants.SECURITY_UPDATE);
2545:                    }
2546:
2547:                    systemSetProperty(uri, key, value);
2548:                } catch (URINotExistException se) {
2549:                    ctx.setRollbackOnly();
2550:                    throw se;
2551:                } catch (LibresourceSecurityException se) {
2552:                    ctx.setRollbackOnly();
2553:                    throw se;
2554:                } catch (Exception e) {
2555:                    ctx.setRollbackOnly();
2556:                    throw new KernelServiceException("Error in setProperty : "
2557:                            + e.getMessage(), e);
2558:                }
2559:            }
2560:
2561:            /**
2562:             * @ejb.interface-method
2563:             * @ejb.transaction type="Required"
2564:             */
2565:            public void systemSetProperty(URI uri, String key, Object value)
2566:                    throws KernelServiceException,
2567:                    LibresourceSecurityException, URINotExistException {
2568:                try {
2569:                    try {
2570:                        NodeLocal node = NodeUtil.getLocalHome().getByPath(
2571:                                normalizeAbsoluteURIPath(uri));
2572:                        HashMap map = node.getProperties();
2573:
2574:                        if (map == null) {
2575:                            map = new HashMap();
2576:                        }
2577:
2578:                        map.put(key, value);
2579:                        node.setProperties(map);
2580:                        node.setUpdateDate(new Date());
2581:
2582:                        // event
2583:                        Libresource.throwEvent(new LibresourceEvent(uri,
2584:                                KernelConstants.SERVICE, "Node",
2585:                                getConnectedResource(),
2586:                                KernelConstants.EVENT_SET_PROPERTY, key + " = "
2587:                                        + value));
2588:                    } catch (ObjectNotFoundException e) {
2589:                        throw new URINotExistException(normalizeURI(uri));
2590:                    }
2591:                } catch (URINotExistException se) {
2592:                    ctx.setRollbackOnly();
2593:                    throw se;
2594:                } catch (LibresourceSecurityException se) {
2595:                    ctx.setRollbackOnly();
2596:                    throw se;
2597:                } catch (Exception e) {
2598:                    ctx.setRollbackOnly();
2599:                    throw new KernelServiceException(
2600:                            "Error in systemSetProperty : " + e.getMessage(), e);
2601:                }
2602:            }
2603:
2604:            /**
2605:             * @ejb.interface-method
2606:             * @ejb.transaction type="Required"
2607:             */
2608:            public void setCreationDate(URI uri, Date date)
2609:                    throws KernelServiceException,
2610:                    LibresourceSecurityException, URINotExistException {
2611:                try {
2612:                    if (!checkSecurity(uri, KernelConstants.SECURITY_UPDATE)) {
2613:                        throw new LibresourceSecurityException(uri,
2614:                                KernelConstants.SECURITY_UPDATE);
2615:                    }
2616:
2617:                    NodeLocal node = NodeUtil.getLocalHome().getByPath(
2618:                            normalizeAbsoluteURIPath(uri));
2619:                    node.setCreationDate(date);
2620:                } catch (URINotExistException se) {
2621:                    ctx.setRollbackOnly();
2622:                    throw se;
2623:                } catch (LibresourceSecurityException se) {
2624:                    ctx.setRollbackOnly();
2625:                    throw se;
2626:                } catch (Exception e) {
2627:                    ctx.setRollbackOnly();
2628:                    throw new KernelServiceException(
2629:                            "Error in setCreationDate : " + e.getMessage(), e);
2630:                }
2631:            }
2632:
2633:            /**
2634:             * @ejb.interface-method
2635:             * @ejb.transaction type="Supports"
2636:             */
2637:            public Date getCreationDate(URI uri) throws KernelServiceException,
2638:                    LibresourceSecurityException, URINotExistException {
2639:                try {
2640:                    if (!checkSecurity(uri, KernelConstants.SECURITY_READ)) {
2641:                        throw new LibresourceSecurityException(uri,
2642:                                KernelConstants.SECURITY_READ);
2643:                    }
2644:
2645:                    NodeLocal node = NodeUtil.getLocalHome().getByPath(
2646:                            normalizeAbsoluteURIPath(uri));
2647:
2648:                    return node.getCreationDate();
2649:                } catch (URINotExistException se) {
2650:                    ctx.setRollbackOnly();
2651:                    throw se;
2652:                } catch (LibresourceSecurityException se) {
2653:                    ctx.setRollbackOnly();
2654:                    throw se;
2655:                } catch (Exception e) {
2656:                    ctx.setRollbackOnly();
2657:                    throw new KernelServiceException(
2658:                            "Error in setCreationDate : " + e.getMessage(), e);
2659:                }
2660:            }
2661:
2662:            /**
2663:             * @ejb.interface-method
2664:             * @ejb.transaction type="Required"
2665:             */
2666:            public void setUpdateDate(URI uri, Date date)
2667:                    throws KernelServiceException,
2668:                    LibresourceSecurityException, URINotExistException {
2669:                try {
2670:                    // Shouldn't be needed
2671:
2672:                    /*
2673:                     * if (!checkSecurity(uri, KernelConstants.SECURITY_KERNEL_READ)) {
2674:                     * throw new LibresourceSecurityException(uri,
2675:                     * KernelConstants.SECURITY_KERNEL_READ); }
2676:                     */
2677:                    NodeLocal node = NodeUtil.getLocalHome().getByPath(
2678:                            normalizeAbsoluteURIPath(uri));
2679:                    node.setUpdateDate(date);
2680:                } catch (URINotExistException se) {
2681:                    ctx.setRollbackOnly();
2682:                    throw se;
2683:                } catch (LibresourceSecurityException se) {
2684:                    ctx.setRollbackOnly();
2685:                    throw se;
2686:                } catch (Exception e) {
2687:                    ctx.setRollbackOnly();
2688:                    throw new KernelServiceException(
2689:                            "Error in setUpdateDate : " + e.getMessage(), e);
2690:                }
2691:            }
2692:
2693:            /**
2694:             * @ejb.interface-method
2695:             * @ejb.transaction type="Supports"
2696:             */
2697:            public Date getUpdateDate(URI uri) throws KernelServiceException,
2698:                    LibresourceSecurityException, URINotExistException {
2699:                try {
2700:                    if (!checkSecurity(uri, KernelConstants.SECURITY_UPDATE)) {
2701:                        throw new LibresourceSecurityException(uri,
2702:                                KernelConstants.SECURITY_UPDATE);
2703:                    }
2704:
2705:                    NodeLocal node = NodeUtil.getLocalHome().getByPath(
2706:                            normalizeAbsoluteURIPath(uri));
2707:
2708:                    return node.getUpdateDate();
2709:                } catch (URINotExistException se) {
2710:                    ctx.setRollbackOnly();
2711:                    throw se;
2712:                } catch (LibresourceSecurityException se) {
2713:                    ctx.setRollbackOnly();
2714:                    throw se;
2715:                } catch (Exception e) {
2716:                    ctx.setRollbackOnly();
2717:                    throw new KernelServiceException(
2718:                            "Error in getUpdateDate : " + e.getMessage(), e);
2719:                }
2720:            }
2721:
2722:            /**
2723:             * @ejb.interface-method
2724:             * @ejb.transaction type="Required"
2725:             */
2726:            public void resetAcls(URI forUri) throws KernelServiceException,
2727:                    LibresourceSecurityException, URINotExistException {
2728:                try {
2729:                    if (!checkIfCanChangeRights(forUri)) {
2730:                        throw new LibresourceSecurityException(forUri,
2731:                                "OWNER or CRUD");
2732:                    }
2733:
2734:                    systemResetAcls(forUri);
2735:
2736:                    // event
2737:                    Libresource.throwEvent(new LibresourceEvent(forUri,
2738:                            KernelConstants.SERVICE, "Node",
2739:                            getConnectedResource(),
2740:                            KernelConstants.EVENT_RESET_ACLS));
2741:                } catch (LibresourceSecurityException se) {
2742:                    ctx.setRollbackOnly();
2743:                    throw se;
2744:                } catch (URINotExistException se) {
2745:                    ctx.setRollbackOnly();
2746:                    throw se;
2747:                } catch (Exception e) {
2748:                    ctx.setRollbackOnly();
2749:                    throw new KernelServiceException(
2750:                            "Error in systemResetAcls : " + e.getMessage(), e);
2751:                }
2752:            }
2753:
2754:            /**
2755:             * @ejb.interface-method
2756:             * @ejb.transaction type="Supports"
2757:             */
2758:            public String normalizeAbsoluteURIPath(URI uri)
2759:                    throws KernelServiceException,
2760:                    LibresourceSecurityException, URINotExistException {
2761:                try {
2762:                    // String thisHost = ((uri.getHost() == null) ? "" : uri.getHost())
2763:                    // + ((uri.getPort() == -1) ? "" : (":" + uri.getPort()));
2764:                    //
2765:                    // if (((uri.getScheme() != null) &&
2766:                    // !uri.getScheme().equals(getUriScheme())) || (!thisHost.equals("")
2767:                    // && !thisHost.equals(getUriHost()))) {
2768:                    // throw new URINotExistException("Invalid URI for this site : " +
2769:                    // uri + " [this site scheme is " + getUriScheme() + "; this site
2770:                    // host is " +
2771:                    // getUriHost() + "]");
2772:                    // }
2773:                    String path = uri.normalize().getPath();
2774:
2775:                    if (!path.startsWith("/")) {
2776:                        path = "/" + path;
2777:                    }
2778:
2779:                    if (path.endsWith("/")) {
2780:                        path = path.substring(0, path.length() - 1);
2781:                    }
2782:
2783:                    if (path.equals("")) {
2784:                        return "/";
2785:                    }
2786:
2787:                    if (path.startsWith("/..")) {
2788:                        throw new URINotExistException("Invalid URI");
2789:                    }
2790:
2791:                    return path;
2792:                } catch (URINotExistException se) {
2793:                    throw se;
2794:                } catch (Exception e) {
2795:                    throw new KernelServiceException(
2796:                            "Error in normalizeAbsoluteURIPath : "
2797:                                    + e.getMessage(), e);
2798:                }
2799:            }
2800:
2801:            /**
2802:             * @ejb.interface-method
2803:             * @ejb.transaction type="Required"
2804:             */
2805:            public void createSymbolicLink(URI uri, URI toUri)
2806:                    throws KernelServiceException, LibresourceSecurityException {
2807:                try {
2808:                    SymbolicLinkResourceLocal linkResourceLocal = SymbolicLinkResourceUtil
2809:                            .getLocalHome().create(
2810:                                    normalizeAbsoluteURIPath(toUri));
2811:                    bind(linkResourceLocal.getLibresourceResourceIdentifier(),
2812:                            uri, getShortName(toUri));
2813:
2814:                    // event
2815:                    LibresourceEvent event = new LibresourceEvent(uri,
2816:                            linkResourceLocal
2817:                                    .getLibresourceResourceIdentifier(),
2818:                            getConnectedResource(),
2819:                            "libresourceCore.symbolicLink.create");
2820:                    Libresource.throwEvent(event);
2821:                } catch (LibresourceSecurityException se) {
2822:                    ctx.setRollbackOnly();
2823:                    throw se;
2824:                } catch (Exception e) {
2825:                    ctx.setRollbackOnly();
2826:                    throw new KernelServiceException(
2827:                            "Error in createSymbolicLink : " + e.getMessage(),
2828:                            e);
2829:                }
2830:            }
2831:
2832:            /**
2833:             * @ejb.interface-method
2834:             * @ejb.transaction type="Required"
2835:             */
2836:            public void editSymbolicLink(URI uri, URI toUri)
2837:                    throws KernelServiceException, LibresourceSecurityException {
2838:                try {
2839:                    LibresourceResourceIdentifier id = lookup(uri);
2840:                    SymbolicLinkResourceLocal linkResourceLocal = (SymbolicLinkResourceLocal) Libresource
2841:                            .findResource(id, SymbolicLinkResourceLocal.class);
2842:                    linkResourceLocal
2843:                            .setLinkURI(normalizeAbsoluteURIPath(toUri));
2844:
2845:                    try {
2846:                        setShortName(uri, Libresource.getShortName(toUri));
2847:                    } catch (Exception e) {
2848:                        setShortName(uri, "Invalid Symbolic Link to " + toUri);
2849:                    }
2850:
2851:                    setUpdateDate(uri, new Date());
2852:
2853:                    // event
2854:                    LibresourceEvent event = new LibresourceEvent(uri,
2855:                            linkResourceLocal
2856:                                    .getLibresourceResourceIdentifier(),
2857:                            getConnectedResource(),
2858:                            "libresourceCore.symbolicLink.edit");
2859:                    Libresource.throwEvent(event);
2860:                } catch (LibresourceSecurityException se) {
2861:                    ctx.setRollbackOnly();
2862:                    throw se;
2863:                } catch (Exception e) {
2864:                    ctx.setRollbackOnly();
2865:                    throw new KernelServiceException(
2866:                            "Error in editSymbolicLink : " + e.getMessage(), e);
2867:                }
2868:            }
2869:
2870:            /**
2871:             * @ejb.interface-method
2872:             * @ejb.transaction type="Supports"
2873:             */
2874:            public SymbolicLinkResourceValue getSymbolicLink(URI uri)
2875:                    throws KernelServiceException, LibresourceSecurityException {
2876:                try {
2877:                    LibresourceResourceIdentifier id = lookup(uri);
2878:                    SymbolicLinkResourceLocal linkResourceLocal = (SymbolicLinkResourceLocal) Libresource
2879:                            .findResource(id, SymbolicLinkResourceLocal.class);
2880:                    SymbolicLinkResourceValue linkResourceValue = linkResourceLocal
2881:                            .getSymbolicLinkResourceValue();
2882:                    linkResourceValue.setUri(normalizeURI(uri));
2883:
2884:                    return linkResourceValue;
2885:                } catch (LibresourceSecurityException se) {
2886:                    throw se;
2887:                } catch (Exception e) {
2888:                    throw new KernelServiceException(
2889:                            "Error in getSymbolicLink : " + e.getMessage(), e);
2890:                }
2891:            }
2892:
2893:            // system
2894:
2895:            /**
2896:             * @ejb.interface-method
2897:             * @ejb.transaction type="Supports"
2898:             */
2899:            public LibresourceResourceIdentifier systemLookup(String path)
2900:                    throws Exception {
2901:                try {
2902:                    NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
2903:                            .getByPath(path);
2904:
2905:                    return LibresourceResourceIdentifier
2906:                            .deserialize(pathNodeLocal
2907:                                    .getBindedResourceIdentifier());
2908:                } catch (ObjectNotFoundException e) {
2909:                    throw new URINotExistException(new URI(path));
2910:                }
2911:            }
2912:
2913:            /**
2914:             * @ejb.interface-method
2915:             * @ejb.transaction type="Supports"
2916:             */
2917:            public LibresourceResourceIdentifier systemLookup(URI uri)
2918:                    throws Exception {
2919:                try {
2920:                    NodeLocal pathNodeLocal = NodeUtil.getLocalHome()
2921:                            .getByPath(normalizeAbsoluteURIPath(uri));
2922:
2923:                    return LibresourceResourceIdentifier
2924:                            .deserialize(pathNodeLocal
2925:                                    .getBindedResourceIdentifier());
2926:                } catch (ObjectNotFoundException e) {
2927:                    throw new URINotExistException(normalizeURI(uri));
2928:                }
2929:            }
2930:
2931:            /**
2932:             * @ejb.interface-method
2933:             * @ejb.transaction type="Required"
2934:             */
2935:            public void systemResetAcls(URI forUri)
2936:                    throws KernelServiceException,
2937:                    LibresourceSecurityException, URINotExistException {
2938:                try {
2939:                    NodeLocal forUriNode;
2940:
2941:                    try {
2942:                        forUriNode = NodeUtil.getLocalHome().getByPath(
2943:                                normalizeAbsoluteURIPath(forUri));
2944:                    } catch (ObjectNotFoundException e) {
2945:                        throw new URINotExistException(normalizeURI(forUri));
2946:                    }
2947:
2948:                    forUriNode.setAcls(new HashMap());
2949:                    forUriNode.setUpdateDate(new Date());
2950:
2951:                    // event
2952:                    Libresource.throwEvent(new LibresourceEvent(forUri,
2953:                            KernelConstants.SERVICE, "Node",
2954:                            getConnectedResource(),
2955:                            KernelConstants.EVENT_RESET_ACLS));
2956:                } catch (LibresourceSecurityException se) {
2957:                    ctx.setRollbackOnly();
2958:                    throw se;
2959:                } catch (URINotExistException se) {
2960:                    ctx.setRollbackOnly();
2961:                    throw se;
2962:                } catch (Exception e) {
2963:                    ctx.setRollbackOnly();
2964:                    throw new KernelServiceException(
2965:                            "Error in systemResetAcls : " + e.getMessage(), e);
2966:                }
2967:            }
2968:
2969:            /**
2970:             * @ejb.interface-method
2971:             * @ejb.transaction type="Required"
2972:             */
2973:            public void systemChown(URI uri, URI owner, boolean recursive)
2974:                    throws KernelServiceException,
2975:                    LibresourceSecurityException, URINotExistException {
2976:                try {
2977:                    NodeLocal uriNode;
2978:                    NodeLocal ownerNode;
2979:
2980:                    try {
2981:                        uriNode = NodeUtil.getLocalHome().getByPath(
2982:                                normalizeAbsoluteURIPath(uri));
2983:                    } catch (ObjectNotFoundException e) {
2984:                        throw new URINotExistException(normalizeURI(uri));
2985:                    }
2986:
2987:                    try {
2988:                        ownerNode = NodeUtil.getLocalHome().getByPath(
2989:                                normalizeAbsoluteURIPath(owner));
2990:                    } catch (ObjectNotFoundException e) {
2991:                        throw new URINotExistException(normalizeURI(owner));
2992:                    }
2993:
2994:                    uriNode.setOwner(ownerNode.getId());
2995:
2996:                    if (recursive) {
2997:                        Collection children = uriNode.getChildren2();
2998:
2999:                        for (Iterator i = children.iterator(); i.hasNext();) {
3000:                            NodeLocal child = (NodeLocal) i.next();
3001:                            chown(child.getURI(), owner, true);
3002:                        }
3003:                    }
3004:
3005:                    setUpdateDate(uri, new Date());
3006:
3007:                    // event
3008:                    Libresource
3009:                            .throwEvent(new LibresourceEvent(uri,
3010:                                    KernelConstants.SERVICE, "Node",
3011:                                    getConnectedResource(),
3012:                                    KernelConstants.EVENT_CHOWN));
3013:                } catch (LibresourceSecurityException se) {
3014:                    ctx.setRollbackOnly();
3015:                    throw se;
3016:                } catch (URINotExistException se) {
3017:                    ctx.setRollbackOnly();
3018:                    throw se;
3019:                } catch (Exception e) {
3020:                    ctx.setRollbackOnly();
3021:                    throw new KernelServiceException("Error in systemChown : "
3022:                            + e.getMessage(), e);
3023:                }
3024:            }
3025:
3026:            /**
3027:             * @ejb.interface-method
3028:             * @ejb.transaction type="Required"
3029:             */
3030:            public void systemCreateURI(URI uri) throws Exception {
3031:                try {
3032:                    NodeLocalHome pathNodeLocalHome = NodeUtil.getLocalHome();
3033:                    StringTokenizer tokenizer = new StringTokenizer(
3034:                            normalizeAbsoluteURIPath(uri), "/");
3035:                    ArrayList pathParts = new ArrayList();
3036:
3037:                    while (tokenizer.hasMoreTokens()) {
3038:                        pathParts.add(tokenizer.nextToken());
3039:                    }
3040:
3041:                    NodeLocal tempParent = null;
3042:
3043:                    try {
3044:                        tempParent = pathNodeLocalHome.findByPrimaryKey("666");
3045:                    } catch (ObjectNotFoundException e) {
3046:                        tempParent = pathNodeLocalHome.create(null, "");
3047:                    }
3048:
3049:                    for (Iterator i = pathParts.iterator(); i.hasNext();) {
3050:                        String pathPart = (String) i.next();
3051:
3052:                        // tempPath.append(pathPart);
3053:                        String childid = (String) tempParent.getChildren().get(
3054:                                pathPart);
3055:
3056:                        if (childid == null) {
3057:                            NodeLocal newNode = pathNodeLocalHome.create(
3058:                                    tempParent, pathPart);
3059:                            copyAcls(tempParent, newNode);
3060:                            tempParent.addChild(newNode);
3061:                            tempParent = newNode;
3062:                        } else {
3063:                            tempParent = pathNodeLocalHome
3064:                                    .findByPrimaryKey(childid);
3065:                        }
3066:                    }
3067:
3068:                    // event
3069:                    // Libresource.throwEvent(new LibresourceEvent(uri, KernelConstants.SERVICE,
3070:                    // "Node", getConnectedResource(), "kernel.create"));
3071:                } catch (Exception e) {
3072:                    ctx.setRollbackOnly();
3073:                    throw e;
3074:                }
3075:            }
3076:
3077:            /**
3078:             * @ejb.interface-method
3079:             * @ejb.transaction type="Required"
3080:             */
3081:            public void systemCopyAcl(URI from, URI to) throws Exception {
3082:                try {
3083:                    NodeLocalHome pathNodeLocalHome = NodeUtil.getLocalHome();
3084:                    copyAcls(pathNodeLocalHome
3085:                            .getByPath(normalizeAbsoluteURIPath(from)),
3086:                            pathNodeLocalHome
3087:                                    .getByPath(normalizeAbsoluteURIPath(to)));
3088:                } catch (Exception e) {
3089:                    ctx.setRollbackOnly();
3090:                    throw e;
3091:                }
3092:            }
3093:
3094:            /**
3095:             * @ejb.interface-method
3096:             * @ejb.transaction type="Required"
3097:             */
3098:            public void systemDeleteChildren(URI uri) throws Exception {
3099:                try {
3100:                    NodeLocalHome pathNodeLocalHome = NodeUtil.getLocalHome();
3101:                    NodeLocal nodeLocal = pathNodeLocalHome
3102:                            .getByPath(normalizeAbsoluteURIPath(uri));
3103:
3104:                    for (Iterator i = nodeLocal.getChildren().values()
3105:                            .iterator(); i.hasNext();) {
3106:                        NodeUtil.getLocalHome().findByPrimaryKey(
3107:                                (String) i.next()).remove();
3108:                    }
3109:                } catch (Exception e) {
3110:                    ctx.setRollbackOnly();
3111:                    throw e;
3112:                }
3113:            }
3114:
3115:            /**
3116:             * @ejb.interface-method
3117:             * @ejb.transaction type="Required"
3118:             */
3119:            public void systemCreateAcl(URI resource, URI owner,
3120:                    String[] permissions) throws Exception {
3121:                try {
3122:                    NodeLocal node = NodeUtil.getLocalHome().getByPath(
3123:                            normalizeAbsoluteURIPath(resource));
3124:                    HashMap acls = node.getAcls();
3125:
3126:                    if (acls == null) {
3127:                        acls = new HashMap();
3128:                    }
3129:
3130:                    // Keep only owner that have at least on right
3131:                    String[] resultingPermission = filterPermissions(
3132:                            LibresourceResourceIdentifier.deserialize(node
3133:                                    .getBindedResourceIdentifier()),
3134:                            permissions);
3135:
3136:                    if (resultingPermission.length > 0) {
3137:                        acls.put(owner, resultingPermission);
3138:                    }
3139:
3140:                    node.setAcls(acls);
3141:                    node.setUpdateDate(new Date());
3142:
3143:                    // event
3144:                    Libresource.throwEvent(new LibresourceEvent(resource,
3145:                            KernelConstants.SERVICE, "Node",
3146:                            getConnectedResource(),
3147:                            KernelConstants.EVENT_CREATE_ACLS));
3148:                } catch (Exception e) {
3149:                    ctx.setRollbackOnly();
3150:                    throw e;
3151:                }
3152:            }
3153:
3154:            /**
3155:             * @ejb.interface-method
3156:             * @ejb.transaction type="Required"
3157:             */
3158:            public void systemBind(
3159:                    LibresourceResourceIdentifier resourceIdentifier, URI uri,
3160:                    String resourceName) throws Exception {
3161:                try {
3162:                    systemCreateURI(uri);
3163:
3164:                    NodeLocalHome pathNodeLocalHome = NodeUtil.getLocalHome();
3165:                    NodeLocal node = pathNodeLocalHome
3166:                            .getByPath(normalizeAbsoluteURIPath(uri));
3167:                    node.setBindedResourceIdentifier(resourceIdentifier
3168:                            .serialize());
3169:                    node.setShortName(resourceName);
3170:                    node.setUpdateDate(new Date());
3171:
3172:                    // event
3173:                    Libresource.throwEvent(new LibresourceEvent(uri,
3174:                            resourceIdentifier, getConnectedResource(),
3175:                            KernelConstants.EVENT_BIND));
3176:                } catch (Exception e) {
3177:                    ctx.setRollbackOnly();
3178:                    throw e;
3179:                }
3180:            }
3181:
3182:            /**
3183:             * @ejb.interface-method
3184:             * @ejb.transaction type="RequiresNew"
3185:             */
3186:            public void systemBootstrap() throws Exception {
3187:                try {
3188:                    NodeLocalHome nodeLocalHome = NodeUtil.getLocalHome();
3189:
3190:                    // create the root node
3191:                    NodeLocal root = nodeLocalHome.create(null, "");
3192:
3193:                    // create the guest user
3194:                    MembershipService membershipService = (MembershipService) Libresource
3195:                            .getService(MembershipConstants.SERVICE);
3196:                    ProfileResourceLocal profileResourceLocal = ProfileResourceUtil
3197:                            .getLocalHome()
3198:                            .create(
3199:                                    membershipService.getUnauthentifiedUserId(),
3200:                                    "Guest", "", "", new HashMap());
3201:                    URI profileUri = new URI(membershipService
3202:                            .getUsersRootURI().getPath()
3203:                            + "/" + membershipService.getUnauthentifiedUserId());
3204:                    systemCreateURI(profileUri);
3205:                    systemBind(profileResourceLocal
3206:                            .getLibresourceResourceIdentifier(), profileUri,
3207:                            "Guest");
3208:                    systemCreateAcl(profileUri, profileUri,
3209:                            new String[] { KernelConstants.SECURITY_READ });
3210:                } catch (Exception e) {
3211:                    ctx.setRollbackOnly();
3212:                    throw e;
3213:                }
3214:            }
3215:
3216:            /**
3217:             * @ejb.interface-method
3218:             * @ejb.transaction type="Required"
3219:             */
3220:            public void systemInitConfig() throws Exception {
3221:                URI configUri = getServerConfigURI();
3222:                systemCreateURI(configUri);
3223:                systemResetAcls(configUri);
3224:
3225:                MembershipService membershipService = (MembershipService) Libresource
3226:                        .getService(MembershipConstants.SERVICE);
3227:                systemCreateAcl(configUri, new URI(membershipService
3228:                        .getUsersRootURI().getPath()
3229:                        + "/" + membershipService.getUnauthentifiedUserId()),
3230:                        new String[] { KernelConstants.SECURITY_READ });
3231:                systemCreateAcl(configUri, new URI(membershipService
3232:                        .getUsersRootURI().getPath()
3233:                        + "/root"), new String[] {
3234:                        KernelConstants.SECURITY_READ,
3235:                        KernelConstants.SECURITY_UPDATE });
3236:                systemSetProperty(configUri, "register-users",
3237:                        membershipService.getUsersRootURI().getPath() + "/"
3238:                                + membershipService.getUnauthentifiedUserId());
3239:            }
3240:
3241:            // helpers
3242:            private Vector sortResourcesList(Vector resourcesList)
3243:                    throws Exception {
3244:                Collections.sort(resourcesList, new Comparator() {
3245:                    public int compare(Object o1, Object o2) {
3246:                        if (!(o1 instanceof  LibresourceResourceValue)) {
3247:                            return 0;
3248:                        }
3249:
3250:                        if ((((LibresourceResourceValue) o1)
3251:                                .getShortResourceName() != null)
3252:                                && (((LibresourceResourceValue) o2)
3253:                                        .getShortResourceName() != null)) {
3254:                            // System.out.println(((LibresourceResourceValue)
3255:                            // o1).getShortResourceName());
3256:                            // System.out.println(((LibresourceResourceValue)
3257:                            // o2).getShortResourceName());
3258:                            // System.out.println(((LibresourceResourceValue)
3259:                            // o1).getShortResourceName()).compareToIgnoreCase(((LibresourceResourceValue)
3260:                            // o2).getShortResourceName()
3261:                            return (((LibresourceResourceValue) o1)
3262:                                    .getShortResourceName())
3263:                                    .compareToIgnoreCase(((LibresourceResourceValue) o2)
3264:                                            .getShortResourceName());
3265:                        }
3266:
3267:                        return 0;
3268:                    }
3269:                });
3270:
3271:                return resourcesList;
3272:            }
3273:
3274:            private Vector sortUrisList(Vector urisList) throws Exception {
3275:                Collections.sort(urisList, new Comparator() {
3276:                    public int compare(Object o1, Object o2) {
3277:                        if (!(o1 instanceof  URI)) {
3278:                            return 0;
3279:                        }
3280:
3281:                        return (((URI) o1).compareTo((URI) o2));
3282:                    }
3283:                });
3284:
3285:                return urisList;
3286:            }
3287:
3288:            private void copyAcls(NodeLocal fromUri, NodeLocal toUri)
3289:                    throws Exception {
3290:                if (fromUri == null) {
3291:                    return;
3292:                }
3293:
3294:                LibresourceResourceIdentifier resourceIdentifier = LibresourceResourceIdentifier
3295:                        .deserialize(toUri.getBindedResourceIdentifier());
3296:
3297:                HashMap acls = new HashMap();
3298:                HashMap parentAcls = fromUri.getAcls();
3299:
3300:                if (parentAcls != null) {
3301:                    for (Iterator i = parentAcls.keySet().iterator(); i
3302:                            .hasNext();) {
3303:                        URI key = (URI) i.next();
3304:                        String[] permissions = (String[]) parentAcls.get(key);
3305:                        acls.put(key, filterPermissions(resourceIdentifier,
3306:                                permissions));
3307:                    }
3308:                }
3309:
3310:                toUri.setAcls(acls);
3311:                toUri.setUpdateDate(new Date());
3312:
3313:                // event
3314:                Libresource.throwEvent(new LibresourceEvent(toUri.getURI(),
3315:                        KernelConstants.SERVICE, "Node",
3316:                        getConnectedResource(),
3317:                        KernelConstants.EVENT_CREATE_ACLS));
3318:            }
3319:
3320:            private String[] filterPermissions(
3321:                    LibresourceResourceIdentifier resourceIdentifier,
3322:                    String[] permissions) throws Exception {
3323:                String[] availablesPermissions = Libresource
3324:                        .listAvailablesPermissions(resourceIdentifier);
3325:                Vector filtered = new Vector();
3326:
3327:                for (int i = 0; i < permissions.length; i++) {
3328:                    if (permissions[i] == null) {
3329:                        continue;
3330:                    }
3331:
3332:                    String permission = permissions[i].trim();
3333:
3334:                    if (permission.startsWith(KernelConstants.SERVICE)
3335:                            || contains(availablesPermissions, permission)) {
3336:                        filtered.add(permission);
3337:                    }
3338:                }
3339:
3340:                String[] result = new String[filtered.size()];
3341:
3342:                for (int i = 0; i < result.length; i++) {
3343:                    result[i] = (String) filtered.get(i);
3344:                }
3345:
3346:                return result;
3347:            }
3348:
3349:            private boolean contains(String[] list, String element) {
3350:                if ((list == null) || (element == null)) {
3351:                    return false;
3352:                }
3353:
3354:                for (int i = 0; i < list.length; i++) {
3355:                    if (list[i] == null) {
3356:                        continue;
3357:                    }
3358:
3359:                    if (list[i].equals(element)) {
3360:                        return true;
3361:                    }
3362:                }
3363:
3364:                return false;
3365:            }
3366:
3367:            public Object getPropertyObject(NodeLocal node, String key)
3368:                    throws Exception {
3369:                if (node == null) {
3370:                    return null;
3371:                }
3372:
3373:                HashMap properties = node.getProperties();
3374:
3375:                if (properties == null) {
3376:                    return null;
3377:                }
3378:
3379:                return properties.get(key);
3380:            }
3381:
3382:            public String getProperty(NodeLocal node, String key)
3383:                    throws Exception {
3384:                return (String) getPropertyObject(node, key);
3385:            }
3386:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.