Clover coverage report - Ledge Components - SNAPSHOT
Coverage timestamp: Fri Nov 17 2006 05:13:20 CET
file stats: LOC: 686   Methods: 36
NCLOC: 482   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DatabaseDirContext.java 92.9% 84% 55.6% 82.2%
coverage coverage
 1    //
 2    //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 3    //All rights reserved.
 4    //
 5    //Redistribution and use in source and binary forms, with or without modification,
 6    //are permitted provided that the following conditions are met:
 7    //
 8    //* Redistributions of source code must retain the above copyright notice,
 9    //this list of conditions and the following disclaimer.
 10    //* Redistributions in binary form must reproduce the above copyright notice,
 11    //this list of conditions and the following disclaimer in the documentation
 12    //and/or other materials provided with the distribution.
 13    //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
 14    //nor the names of its contributors may be used to endorse or promote products
 15    //derived from this software without specific prior written permission.
 16    //
 17    //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 18    //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 19    //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 20    //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 21    //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 22    //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 23    //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 24    //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25    //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26    //POSSIBILITY OF SUCH DAMAGE.
 27    //
 28   
 29    package org.objectledge.naming.db;
 30   
 31    import java.sql.Connection;
 32    import java.sql.ResultSet;
 33    import java.sql.SQLException;
 34    import java.sql.Statement;
 35    import java.util.ArrayList;
 36    import java.util.HashMap;
 37    import java.util.Hashtable;
 38    import java.util.Iterator;
 39    import java.util.List;
 40    import java.util.Map;
 41   
 42    import javax.naming.CompositeName;
 43    import javax.naming.Context;
 44    import javax.naming.Name;
 45    import javax.naming.NamingEnumeration;
 46    import javax.naming.NamingException;
 47    import javax.naming.directory.Attribute;
 48    import javax.naming.directory.Attributes;
 49    import javax.naming.directory.BasicAttribute;
 50    import javax.naming.directory.BasicAttributes;
 51    import javax.naming.directory.DirContext;
 52    import javax.naming.directory.ModificationItem;
 53    import javax.naming.directory.SearchControls;
 54    import javax.naming.directory.SearchResult;
 55   
 56    import org.objectledge.database.DatabaseUtils;
 57    import org.objectledge.database.persistence.Persistence;
 58    import org.objectledge.database.persistence.PersistenceException;
 59   
 60    /**
 61    * Database based implementation of java.naming.directory.DirContext interface.
 62    *
 63    * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
 64    */
 65    public class DatabaseDirContext extends DatabaseContext implements DirContext
 66    {
 67    /**
 68    * The directory context constructor.
 69    *
 70    * @param env the environment.
 71    */
 72  920 public DatabaseDirContext(Hashtable env)
 73    {
 74  920 super(env);
 75    }
 76   
 77    /**
 78    * The context constructor.
 79    *
 80    * @param env the environment.
 81    * @param context the persistent context delegate.
 82    * @param persistence the persistence.
 83    * @throws NamingException if operation failed.
 84    */
 85  7268 protected DatabaseDirContext(Hashtable env, PersistentContext context, Persistence persistence)
 86    throws NamingException
 87    {
 88  7268 super(env, context, persistence);
 89    }
 90   
 91    /**
 92    * {@inheritDoc}
 93    */
 94  6992 public Object lookup(Name name) throws NamingException
 95    {
 96  6992 if (name.isEmpty())
 97    {
 98  6348 return new DatabaseDirContext(env, context, persistence);
 99    }
 100  644 String dn = getDN(name);
 101  644 List list = lookupContext(dn);
 102  644 if(list.size() == 0)
 103    {
 104  184 throw new NamingException("faled to retrieve context '"+dn+"' from database");
 105    }
 106  460 if(list.size() > 1)
 107    {
 108  0 throw new NamingException("ambigious context '"+dn+"' in database");
 109    }
 110  460 return new DatabaseDirContext(env, (PersistentContext)list.get(0), persistence);
 111    }
 112   
 113    /**
 114    * {@inheritDoc}
 115    */
 116  828 public Attributes getAttributes(Name name) throws NamingException
 117    {
 118  828 return getAttributes(name, null);
 119    }
 120   
 121    /**
 122    * {@inheritDoc}
 123    */
 124  828 public Attributes getAttributes(String name) throws NamingException
 125    {
 126  828 return getAttributes(new CompositeName(name));
 127    }
 128   
 129    /**
 130    * {@inheritDoc}
 131    */
 132  2392 public Attributes getAttributes(Name name, String[] attrIds) throws NamingException
 133    {
 134  2392 DatabaseDirContext ctx = (DatabaseDirContext)lookup(name);
 135  2392 long contextId = ctx.getDelegate().getContextId();
 136  2392 Attributes allAttributes = getAllAttributes(contextId);
 137  2392 return filterAttributes(allAttributes, attrIds);
 138    }
 139   
 140    /**
 141    * {@inheritDoc}
 142    */
 143  1564 public Attributes getAttributes(String name, String[] attrIds) throws NamingException
 144    {
 145  1564 return getAttributes(new CompositeName(name), attrIds);
 146    }
 147   
 148    /**
 149    * {@inheritDoc}
 150    */
 151  782 public void modifyAttributes(Name name, int modOp, Attributes attrs) throws NamingException
 152    {
 153  782 if (attrs == null || attrs.size() == 0)
 154    {
 155  0 return;
 156    }
 157  782 NamingEnumeration enumerator = attrs.getAll();
 158  782 ModificationItem[] items = new ModificationItem[attrs.size()];
 159  782 int i = 0;
 160  782 while(enumerator.hasMoreElements())
 161    {
 162  1748 items[i++] = new ModificationItem(modOp, (Attribute)enumerator.next());
 163    }
 164  782 modifyAttributes(name, items);
 165    }
 166   
 167    /**
 168    * {@inheritDoc}
 169    */
 170  782 public void modifyAttributes(String name, int modOp, Attributes attrs)
 171    throws NamingException
 172    {
 173  782 modifyAttributes(new CompositeName(name), modOp, attrs);
 174    }
 175   
 176    /**
 177    * {@inheritDoc}
 178    */
 179  2162 public void modifyAttributes(Name name, ModificationItem[] mods) throws NamingException
 180    {
 181  2162 List failures = new ArrayList();
 182  2162 DatabaseDirContext ctx = (DatabaseDirContext)lookup(name);
 183  2162 boolean transactionControler;
 184  2162 try
 185    {
 186  2162 transactionControler = persistence.getDatabase().beginTransaction();
 187    }
 188    catch(SQLException e)
 189    {
 190  0 throw new RuntimeException("Failed to begin transaction",e);
 191    }
 192  2162 for (int i=0; i<mods.length; i++)
 193    {
 194  3588 Attribute attribute = mods[i].getAttribute();
 195  3588 NamingEnumeration values = attribute.getAll();
 196  3588 switch (mods[i].getModificationOp())
 197    {
 198  2622 case ADD_ATTRIBUTE:
 199  2622 try
 200    {
 201  2622 while (values.hasMore())
 202    {
 203  3036 String value = (String)values.next();
 204  3036 PersistentAttribute pAttribute = new PersistentAttribute(
 205    ctx.getDelegate().getContextId(),
 206    attribute.getID(), value);
 207  3036 persistence.save(pAttribute);
 208    }
 209    }
 210    catch(PersistenceException e)
 211    {
 212  0 failures.add(mods[i]);
 213    }
 214  2622 break;
 215  92 case REPLACE_ATTRIBUTE:
 216  92 try
 217    {
 218  92 StringBuilder sb = new StringBuilder();
 219  92 sb.append("context_id = ");
 220  92 sb.append(ctx.getDelegate().getContextId());
 221  92 sb.append(" and name = '");
 222  92 sb.append(attribute.getID());
 223  92 sb.append("'");
 224  92 persistence.delete(sb.toString(), PersistentAttribute.FACTORY);
 225  92 while (values.hasMore())
 226    {
 227  92 String value = (String)values.next();
 228  92 PersistentAttribute pAttribute = new PersistentAttribute(
 229    ctx.getDelegate().getContextId(),
 230    attribute.getID(), value);
 231  92 persistence.save(pAttribute);
 232    }
 233    }
 234    catch(PersistenceException e)
 235    {
 236  0 failures.add(mods[i]);
 237    }
 238  92 break;
 239  874 case REMOVE_ATTRIBUTE:
 240  874 try
 241    {
 242  874 if(values.hasMore())
 243    {
 244  414 while(values.hasMore())
 245    {
 246  644 String value = (String)values.next();
 247  644 StringBuilder sb = new StringBuilder();
 248  644 sb.append("context_id = ");
 249  644 sb.append(ctx.getDelegate().getContextId());
 250  644 sb.append(" and name = '");
 251  644 sb.append(attribute.getID());
 252  644 sb.append("' and value = '");
 253  644 sb.append(value);
 254  644 sb.append("'");
 255  644 persistence.delete(sb.toString(), PersistentAttribute.FACTORY);
 256    }
 257    }
 258    else
 259    {
 260  460 StringBuilder sb = new StringBuilder();
 261  460 sb.append("context_id = ");
 262  460 sb.append(ctx.getDelegate().getContextId());
 263  460 sb.append(" and name = '");
 264  460 sb.append(attribute.getID());
 265  460 sb.append("'");
 266  460 persistence.delete(sb.toString(), PersistentAttribute.FACTORY);
 267    }
 268    }
 269    catch(PersistenceException e)
 270    {
 271  0 failures.add(mods[i]);
 272    }
 273   
 274  874 break;
 275  0 default:
 276  0 break;
 277    }
 278    }
 279  2162 if( failures.size() > 0)
 280    {
 281  0 try
 282    {
 283  0 persistence.getDatabase().rollbackTransaction(transactionControler);
 284    }
 285    catch(SQLException e)
 286    {
 287  0 throw new RuntimeException("Failed to begin transaction",e);
 288    }
 289    }
 290    else
 291    {
 292  2162 try
 293    {
 294  2162 persistence.getDatabase().commitTransaction(transactionControler);
 295    }
 296    catch(SQLException e)
 297    {
 298  0 throw new RuntimeException("Failed to begin transaction",e);
 299    }
 300    }
 301    }
 302   
 303    /**
 304    * {@inheritDoc}
 305    */
 306  1380 public void modifyAttributes(String name, ModificationItem[] mods)
 307    throws NamingException
 308    {
 309  1380 modifyAttributes(new CompositeName(name), mods);
 310    }
 311   
 312    /**
 313    * {@inheritDoc}
 314    */
 315  0 public void bind(Name name, Object obj, Attributes attrs) throws NamingException
 316    {
 317  0 throw new UnsupportedOperationException();
 318    }
 319   
 320    /**
 321    * {@inheritDoc}
 322    */
 323  0 public void bind(String name, Object obj, Attributes attrs) throws NamingException
 324    {
 325  0 throw new UnsupportedOperationException();
 326    }
 327   
 328    /**
 329    * {@inheritDoc}
 330    */
 331  0 public void rebind(Name name, Object obj, Attributes attrs) throws NamingException
 332    {
 333  0 throw new UnsupportedOperationException();
 334    }
 335   
 336    /**
 337    * {@inheritDoc}
 338    */
 339  0 public void rebind(String name, Object obj, Attributes attrs) throws NamingException
 340    {
 341  0 throw new UnsupportedOperationException();
 342    }
 343   
 344    /**
 345    * {@inheritDoc}
 346    */
 347  46 public void destroySubcontext(Name name) throws NamingException
 348    {
 349  46 DatabaseContext ctx = (DatabaseContext)lookup(name);
 350  46 long contextId = ctx.getDelegate().getContextId();
 351  46 deleteAllAttributes(contextId);
 352  46 super.destroySubcontext(name);
 353    }
 354   
 355    /**
 356    * {@inheritDoc}
 357    */
 358  506 public DirContext createSubcontext(Name name, Attributes attrs) throws NamingException
 359    {
 360  506 PersistentContext delegate = createContextDelegate(name);
 361  460 DatabaseDirContext subContext = new DatabaseDirContext(env, delegate, persistence);
 362  460 if (attrs != null && attrs.size() > 0)
 363    {
 364  414 subContext.modifyAttributes("", DirContext.ADD_ATTRIBUTE, attrs);
 365    }
 366  460 return subContext;
 367    }
 368   
 369    /**
 370    * {@inheritDoc}
 371    */
 372  46 public DirContext createSubcontext(String name, Attributes attrs) throws NamingException
 373    {
 374  46 return createSubcontext(new CompositeName(name),attrs);
 375    }
 376   
 377    /**
 378    * {@inheritDoc}
 379    */
 380  0 public Context createSubcontext(String name) throws NamingException
 381    {
 382  0 return createSubcontext(name,null);
 383    }
 384   
 385    /**
 386    * {@inheritDoc}
 387    */
 388  0 public Context createSubcontext(Name name) throws NamingException
 389    {
 390  0 return createSubcontext(name, null);
 391    }
 392   
 393   
 394    /**
 395    * {@inheritDoc}
 396    */
 397  0 public DirContext getSchema(Name name) throws NamingException
 398    {
 399  0 throw new UnsupportedOperationException();
 400    }
 401   
 402    /**
 403    * {@inheritDoc}
 404    */
 405  0 public DirContext getSchema(String name) throws NamingException
 406    {
 407  0 return getSchema(new CompositeName(name));
 408    }
 409   
 410    /**
 411    * {@inheritDoc}
 412    */
 413  0 public DirContext getSchemaClassDefinition(Name name) throws NamingException
 414    {
 415  0 throw new UnsupportedOperationException();
 416    }
 417   
 418    /**
 419    * {@inheritDoc}
 420    */
 421  0 public DirContext getSchemaClassDefinition(String name) throws NamingException
 422    {
 423  0 return getSchemaClassDefinition(new CompositeName(name));
 424    }
 425   
 426    /**
 427    * {@inheritDoc}
 428    */
 429  184 public NamingEnumeration search(Name name, Attributes matchingAttributes,
 430    String[] attributesToReturn)
 431    throws NamingException
 432    {
 433  184 List searchResult = new ArrayList();
 434  184 DatabaseDirContext ctx = (DatabaseDirContext)lookup(name);
 435  184 long contextId = ctx.getDelegate().getContextId();
 436  184 Map attributeMap = getChildrenAttributes(contextId);
 437  184 Iterator it = attributeMap.keySet().iterator();
 438  184 while(it.hasNext())
 439    {
 440  184 String dn = (String)it.next();
 441  184 Attributes attributes = (Attributes)attributeMap.get(dn);
 442  184 NamingEnumeration enumeration = matchingAttributes.getAll();
 443  184 if(attributes == null && enumeration.hasMore())
 444    {
 445  0 break;
 446    }
 447  184 boolean found = true;
 448  184 outer: while(enumeration.hasMore())
 449    {
 450  184 Attribute searchAttr = (Attribute)enumeration.next();
 451  184 Attribute attr = (Attribute)attributes.get(searchAttr.getID());
 452  184 if(attr == null)
 453    {
 454  46 found = false;
 455  46 break;
 456    }
 457  138 NamingEnumeration values = searchAttr.getAll();
 458  138 while (values.hasMore())
 459    {
 460  138 String value = (String)values.next();
 461  138 if(attr.contains(value))
 462    {
 463  92 break outer;
 464    }
 465