javamanager/javaregistry/legacy/server/src/javapropertyarray.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2005-2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  javapropertyarray implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32mem.h>
       
    20 #include "javaregproperty.h"
       
    21 #include "javapropertyarray.h"
       
    22 #include "logger.h"
       
    23 
       
    24 // using namespace Java::Utilities;
       
    25 using namespace Java::Manager::Registry;
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 // Constructs a java property array object.
       
    29 // ---------------------------------------------------------------------------
       
    30 
       
    31 CJavaPropertyArray* CJavaPropertyArray::NewL()
       
    32 {
       
    33     CJavaPropertyArray* self = NewLC();
       
    34     CleanupStack::Pop();
       
    35     return self;
       
    36 }
       
    37 
       
    38 
       
    39 // ---------------------------------------------------------------------------
       
    40 // Constructs a java property array object.
       
    41 // ---------------------------------------------------------------------------
       
    42 CJavaPropertyArray* CJavaPropertyArray::NewLC()
       
    43 {
       
    44     CJavaPropertyArray* self = new(ELeave) CJavaPropertyArray();
       
    45     CleanupStack::PushL(self);
       
    46     self->ConstructL(0);
       
    47     return self;
       
    48 }
       
    49 
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 // Constructs a java property array object from a buffer.
       
    53 // ---------------------------------------------------------------------------
       
    54 CJavaPropertyArray* CJavaPropertyArray::NewL(TDesC8*  aBuffer)
       
    55 {
       
    56     CJavaPropertyArray* self = NewLC(aBuffer);
       
    57     CleanupStack::Pop();
       
    58     return self;
       
    59 }
       
    60 
       
    61 
       
    62 // ---------------------------------------------------------------------------
       
    63 // Constructs a java property array object from a buffer.
       
    64 // ---------------------------------------------------------------------------
       
    65 CJavaPropertyArray* CJavaPropertyArray::NewLC(TDesC8*  aBuffer)
       
    66 {
       
    67     CJavaPropertyArray* self = new(ELeave) CJavaPropertyArray();
       
    68     CleanupStack::PushL(self);
       
    69     self->ConstructL(aBuffer);
       
    70     return self;
       
    71 }
       
    72 
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // ConstructL
       
    76 // ---------------------------------------------------------------------------
       
    77 void CJavaPropertyArray::ConstructL(TDesC8*  aBuffer)
       
    78 {
       
    79     if (aBuffer)
       
    80     {
       
    81         RDesReadStream  stream(*aBuffer);
       
    82         CleanupClosePushL(stream);
       
    83         stream >> *this;
       
    84         CleanupStack::PopAndDestroy();  // stream
       
    85     }
       
    86 }
       
    87 
       
    88 
       
    89 // ---------------------------------------------------------------------------
       
    90 // Destructor.
       
    91 // ---------------------------------------------------------------------------
       
    92 CJavaPropertyArray::~CJavaPropertyArray()
       
    93 {
       
    94     if (iPropertiesOwned)
       
    95     {
       
    96         DeleteProperties();
       
    97     }
       
    98     iProperties.Close();
       
    99 }
       
   100 
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // Appends a new property object reference.
       
   104 // ---------------------------------------------------------------------------
       
   105 TInt CJavaPropertyArray::Append(CJavaProperty*  aProperty)
       
   106 {
       
   107     return iProperties.Append(aProperty);
       
   108 }
       
   109 
       
   110 
       
   111 // ---------------------------------------------------------------------------
       
   112 // Returns the total number of contained property object references.
       
   113 // ---------------------------------------------------------------------------
       
   114 TInt CJavaPropertyArray::Count()
       
   115 {
       
   116     return iProperties.Count();
       
   117 }
       
   118 
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // Returns the property object reference located at a specified position.
       
   122 // ---------------------------------------------------------------------------
       
   123 CJavaProperty* CJavaPropertyArray::At(TInt aIndex)
       
   124 {
       
   125     return iProperties[aIndex];
       
   126 }
       
   127 
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // Internalizes the property objects.
       
   131 // ---------------------------------------------------------------------------
       
   132 void CJavaPropertyArray::InternalizeL(RReadStream&  aStream)
       
   133 {
       
   134     // properties are created internally
       
   135     iPropertiesOwned = ETrue;
       
   136 
       
   137     // if there are already contained properties delete them and reset array
       
   138     if (Count())
       
   139     {
       
   140         LOG(EJavaStorage, EInfo,
       
   141             "PropertyArray already contained properties, delete them.");
       
   142         DeleteProperties();
       
   143     }
       
   144 
       
   145     // get the number of properties
       
   146     TInt32 count;
       
   147     aStream >> count;
       
   148 
       
   149     // internalize poperties
       
   150     for (TInt propIndex = 0; propIndex < count; propIndex++)
       
   151     {
       
   152         TInt32 id;
       
   153         HBufC* value;
       
   154         TInt32 valueLength;
       
   155 
       
   156         aStream >> id >> valueLength;
       
   157         value = HBufC::NewLC(aStream, valueLength);
       
   158 
       
   159         CJavaProperty* prop = CJavaProperty::NewL(id, *value);
       
   160         CleanupStack::PopAndDestroy();  // value
       
   161         TInt err = Append(prop);
       
   162 
       
   163         // in case of error delete property objects and leave
       
   164         if (err)
       
   165         {
       
   166             ELOG1(EJavaStorage,
       
   167                   "Error on appending property, delete properties. Error: %d.", err);
       
   168             // delete not appended property
       
   169             delete prop;
       
   170             // delete appended properties
       
   171             DeleteProperties();
       
   172             User::Leave(err);
       
   173         }
       
   174     }
       
   175 }
       
   176 
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // Externalizes the property objects.
       
   180 // ---------------------------------------------------------------------------
       
   181 void CJavaPropertyArray::ExternalizeL(RWriteStream&  aStream) const
       
   182 {
       
   183     // first externalize the number of properties
       
   184     TInt32 count = iProperties.Count();
       
   185     aStream << count;
       
   186 
       
   187     // externalize poperties
       
   188     for (TInt propIndex = 0; propIndex < count; propIndex++)
       
   189     {
       
   190         CJavaProperty* prop = iProperties[propIndex];
       
   191         TInt32 valueLength = prop->Value().Length();
       
   192         aStream << prop->Id() << valueLength << prop->Value();
       
   193     }
       
   194 }
       
   195 
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // Calculates the number of bytes needed for serialization of
       
   199 // the property objects.
       
   200 // ---------------------------------------------------------------------------
       
   201 TInt CJavaPropertyArray::Size() const
       
   202 {
       
   203     // size of the integer containing the total number of properties
       
   204     TInt size = sizeof(TInt32);
       
   205 
       
   206     // increment size with size of properties
       
   207     for (TInt propIndex = 0; propIndex < iProperties.Count(); propIndex++)
       
   208     {
       
   209         CJavaProperty* prop = iProperties[propIndex];
       
   210 
       
   211         // property id size + size of integer containing the value length
       
   212         // + size of property value
       
   213         size += sizeof(prop->Id()) +
       
   214                 sizeof(TInt32) +
       
   215                 prop->Value().Size();
       
   216     }
       
   217 
       
   218     return size;
       
   219 }
       
   220 
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Contains
       
   224 // ---------------------------------------------------------------------------
       
   225 TBool CJavaPropertyArray::Contains(CJavaProperty& aProperty) const
       
   226 {
       
   227     for (TInt propIndex = 0; propIndex < iProperties.Count(); propIndex++)
       
   228     {
       
   229         CJavaProperty* prop = iProperties[propIndex];
       
   230 
       
   231         if (*prop == aProperty)
       
   232         {
       
   233             return ETrue;
       
   234         }
       
   235     }
       
   236 
       
   237     return EFalse;
       
   238 }
       
   239 
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // CJavaPropertyArray::Find
       
   243 // ---------------------------------------------------------------------------
       
   244 TInt CJavaPropertyArray::Find(TInt32 aPropertyId)
       
   245 {
       
   246     for (TInt propIndex = 0; propIndex < iProperties.Count(); propIndex++)
       
   247     {
       
   248         CJavaProperty* prop = iProperties[propIndex];
       
   249 
       
   250         if (prop->Id() == aPropertyId)
       
   251         {
       
   252             return propIndex;
       
   253         }
       
   254     }
       
   255 
       
   256     return KErrNotFound;
       
   257 }
       
   258 
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // Serializes the property objects in a buffer.
       
   262 // ---------------------------------------------------------------------------
       
   263 HBufC8* CJavaPropertyArray::SerializedPropertiesL() const
       
   264 {
       
   265     HBufC8* buffer = HBufC8::NewLC(Size());
       
   266 
       
   267     TPtr8 des8 = buffer->Des();
       
   268     RDesWriteStream stream(des8);
       
   269     CleanupClosePushL(stream);
       
   270 
       
   271     stream << *this;
       
   272     stream.CommitL();
       
   273     CleanupStack::PopAndDestroy();  // stream
       
   274     CleanupStack::Pop();            // buffer
       
   275 
       
   276     return buffer;
       
   277 }
       
   278 
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // Delete and remove property objects.
       
   282 // ---------------------------------------------------------------------------
       
   283 void CJavaPropertyArray::DeleteProperties()
       
   284 {
       
   285     for (TInt i = 0; i < Count(); i++)
       
   286     {
       
   287         delete iProperties[i];
       
   288         iProperties[i] = NULL;
       
   289     }
       
   290     iProperties.Reset();
       
   291 }