Package nexj.core.runtime

Examples of nexj.core.runtime.Instance


            attribute.getName(),
            attribute.getMetaclass().getName()
         });
      }

      Instance obj = instantiate((TransferObject)value);

      if (!attribute.getType().isUpcast(obj.getLazyMetaclass()))
      {
         throw new RequestException("err.rpc.classCast", new Object[]
         {
            obj.getLazyClassName(),
            attribute.getName(),
            attribute.getMetaclass().getName()
         });
      }
View Full Code Here


    * @param tobj The transfer object to instantiate.
    * @return The instance corresponding to the transfer object.
    */
   public Instance instantiate(TransferObject tobj)
   {
      Instance instance = (Instance)m_identityMap.get(tobj);

      if (instance != null)
      {
         return instance;
      }

      Metaclass metaclass = m_context.getMetadata().getMetaclass(tobj.getClassName());
      Attribute lockingAttribute = (metaclass.getPersistenceMapping() != null) ? metaclass.getPersistenceMapping()
         .getLockingAttribute() : null;
      OID oid = tobj.getOID();

      if (isCached())
      {
         if (oid == null)
         {
            instance = null;
         }
         else
         {
            instance = m_context.lockInstance(metaclass, oid, tobj.getEventName() != null);

            if (instance != null && instance.isLazy())
            {
               if (tobj.getVersion() >= 0)
               {
                  instance.setMetaclass(metaclass);
               }
               else if (instance.getLazyMetaclass().isUpcast(metaclass))
               {
                  instance.setLazyMetaclass(metaclass);
               }
            }
         }

         if ("create".equals(tobj.getEventName()))
         {
            if (instance == null)
            {
               instance = new Instance(metaclass, m_context);
               instance.setNew();
               instance.setOID(oid);
            }
         }
         else
         {
            if (instance == null)
            {
               instance = new Instance(metaclass, tobj.getVersion() < 0, m_context);
               m_context.getUnitOfWork().lock(instance.cache(oid), tobj.getEventName() != null);
            }
            else if ((m_nMode & CACHE) != 0 && instance.isCached())
            {
               return instance;
            }
         }

         if ((m_nMode & CACHE) != 0)
         {
            instance.setCached(true);
         }
      }
      else
      {
         if (oid == null)
         {
            instance = new Instance(metaclass, m_context);
            instance.setNew();
            instance.getUnitOfWork().keepChange(instance);
         }
         else
         {
            if (m_context.isProtected() && m_context.isSecure())
            {
               metaclass.checkReadAccess(m_context.getPrivilegeSet());
            }

            instance = m_context.lockInstance(metaclass, oid, tobj.getEventName() != null);

            if (instance == null)
            {
               PersistenceMapping mapping = metaclass.getPersistenceMapping();

               if (mapping != null)
               {
                  Object[] valueArray = oid.getValueArray();
                  Key key = mapping.getObjectKey();

                  if (key.getPartCount() != valueArray.length)
                  {
                     throw new RequestException("err.rpc.oidPartCount", new Object[]
                     {
                        metaclass.getName()
                     });
                  }

                  for (int i = 0; i < valueArray.length; ++i)
                  {
                     valueArray[i] = key.getPartType(i).convert(valueArray[i]);
                  }
               }

               instance = new Instance(metaclass, m_context);
               m_context.getUnitOfWork().lock(instance.cache(oid), tobj.getEventName() != null);
            }
         }
      }

      m_identityMap.put(tobj, instance);

      if ((m_nMode & STATE) != 0 && tobj.getVersion() >= 0)
      {
         instance.load();

         if (lockingAttribute != null && instance.getState() != Instance.NEW)
         {
            Object oldValue = instance.getOldValueDirect(lockingAttribute.getOrdinal());

            if (!(oldValue instanceof Undefined))
            {
               Object value = tobj.findValue(lockingAttribute.getName());

               if (value != null && !ObjUtil.equal(lockingAttribute.getType().convert(value), oldValue))
               {
                  m_bLockMismatch = true;

                  if ((m_nMode & LOCK) != 0)
                  {
                     throw new OptimisticLockException(instance);
                  }

                  if (isCached())
                  {
                     return instance;
                  }

                  if (m_context.isLocked(instance))
                  {
                     throw new OptimisticLockException(instance);
                  }
               }
            }
         }

         TransferObject preObj = null;

         if ((m_nMode & PRE) != 0)
         {
            Object value = tobj.findValue(PRE_NAME);

            if (value != null)
            {
               if (value instanceof TransferObject)
               {
                  preObj = (TransferObject)value;
               }
               else
               {
                  throw new RequestException("err.rpc.preObjectType");
               }
            }
         }

         for (PropertyIterator itr = tobj.getIterator(); itr.hasNext();)
         {
            String sName = (String)itr.next();

            if ((m_nMode & PRE) != 0 && sName.equals(PRE_NAME))
            {
               continue;
            }

            Attribute attribute = metaclass.getAttribute(sName);
            Object value = itr.getValue();

            if (attribute.isStatic())
            {
               throw new RequestException("err.rpc.staticAttribute", new Object[]
               {
                  attribute.getName(),
                  metaclass.getName()
               });
            }

            if (isCached())
            {
               if (!(instance.getOldValueDirect(attribute.getOrdinal()) instanceof Undefined))
               {
                  continue;
               }
            }
            else
            {
               if (!attribute.isCached())
               {
                  throw new RequestException("err.rpc.uncachedAttribute", new Object[]
                  {
                     attribute.getName(),
                     metaclass.getName()
                  });
               }

               if (m_context.isProtected() && m_context.isSecure())
               {
                  attribute.checkReadAccess(m_context.getPrivilegeSet());
               }

               instance.checkUpdateAccess(attribute);
            }

            Object pre = (preObj == null) ? Undefined.VALUE : preObj.findValue(sName, Undefined.VALUE);

            if (attribute.getType().isPrimitive())
            {
               value = instantiatePrimitive(value, attribute);

               if (pre != Undefined.VALUE)
               {
                  pre = instantiatePrimitive(pre, attribute);
               }
            }
            else
            {
               if (attribute.isCollection())
               {
                  InstanceList instanceList;

                  if (pre != Undefined.VALUE)
                  {
                     pre = instanceList = instantiateList(pre, attribute);

                     if (isCached())
                     {
                        instanceList.setLazy(false);
                     }
                  }

                  value = instanceList = instantiateList(value, attribute);

                  if (isCached())
                  {
                     instanceList.setLazy(false);
                  }
                  else
                  {
                     if (attribute.getReverse() != null)
                     {
                        instanceList.checkUpdateAccess(attribute.getReverse(), instance);
                     }
                  }

                  instanceList.setAssociation(instance, attribute, true);
               }
               else
               {
                  Instance inst;

                  if (pre != Undefined.VALUE)
                  {
                     pre = inst = instantiateObject(pre, attribute);
                  }

                  value = inst = instantiateObject(value, attribute);

                  if (isCached() && attribute.getReverse() != null && inst != null && !inst.isLazy())
                  {
                     inst.associate(attribute.getReverse().getOrdinal(), instance, true);
                  }
               }
            }

            if (pre != Undefined.VALUE && ObjUtil.equal(value, pre))
View Full Code Here

         m_context.setUnitOfWorkGlobal(false);

         // Invalidate the dependencies
         for (int i = 0, n = fixupList.size(); i != n; i += 4)
         {
            Instance instance = (Instance)fixupList.get(i);
            Attribute attribute = (Attribute)fixupList.get(i + 1);

            if (attribute.getInverseDependency() != null)
            {
               attribute.invalidateDependency(instance, Invalid.VALUE);
            }

            Attribute reverse = attribute.getReverse();

            if (reverse != null && attribute.isReverseOf(reverse) && reverse.getInverseDependency() != null)
            {
               Object value = fixupList.get(i + 2);

               if (value != null)
               {
                  if (attribute.isCollection())
                  {
                     InstanceList list = (InstanceList)value;

                     for (int k = 0, m = list.getCount(); k != m; ++k)
                     {
                        reverse.invalidateDependency(list.getInstance(k), Invalid.VALUE);
                     }
                  }
                  else
                  {
                     reverse.invalidateDependency((Instance)value, Invalid.VALUE);
                  }
               }
            }
         }

         // Assign the values
         for (int i = 0, n = fixupList.size(); i != n; i += 4)
         {
            Instance instance = (Instance)fixupList.get(i);
            Attribute attribute = (Attribute)fixupList.get(i + 1);
            Object value = fixupList.get(i + 2);
            Object pre = fixupList.get(i + 3);

            instance.assign(attribute, value, true);

            if (pre != Undefined.VALUE)
            {
               InvocationContext context = instance.getInvocationContext();
               byte nGenerationSaved = context.getGeneration();

               try
               {
                  context.setGeneration(InvocationContext.GEN_PRE);
                  instance.assign(attribute, pre, true);
               }
               finally
               {
                  context.setGeneration(nGenerationSaved);
               }
View Full Code Here

      for (Lookup.Iterator itr = identityMap.iterator(); itr.hasNext();)
      {
         if (itr.next() instanceof TransferObject)
         {
            TransferObject tobj = (TransferObject)itr.getKey();
            Instance instance = (Instance)itr.getValue();
            Event pendingEvent = null;
            Event event = null;

            if (instance != null && instance.isEventPending())
            {
               pendingEvent = (Event)instance.getMetaclass().getSelector(instance.getPendingEventName()).getMember(0);
            }

            if (eventMap != null)
            {
               event = (Event)eventMap.get(tobj);
            }
            else if (instance != null && tobj.getEventName() != null)
            {
               event = instance.getMetaclass().findEvent(tobj.getEventName(), 0);
            }

            if (pendingEvent != null && pendingEvent != event)
            {
               RPCUtil.audit(instance, tobj, pendingEvent, context);
View Full Code Here

   {
      for (Lookup.Iterator itr = identityMap.iterator(); itr.hasNext();)
      {
         if (itr.next() instanceof Instance)
         {
            Instance instance = (Instance)itr.getKey();
            TransferObject tobj = (TransferObject)itr.getValue();
  
            RPCUtil.audit(instance, tobj, (Event)instance.getMetaclass().getSelector("read").getMember(6), context);
         }
      }
   }
View Full Code Here

                  (m_bDistributed) ? Pair.attribute("next").le(new Timestamp(lCurrentTime + m_lInterval)) : null,
                  orderBy, m_nBatchCount, 0, true, Query.SEC_NONE, context).read();

               for (int i = 0; i < list.size(); ++i)
               {
                  Instance timer = list.getInstance(i);
                  long lNextTimeout = ((Timestamp)timer.getValue("next")).getTime();

                  if (lNextTimeout <= lCurrentTime)
                  {
                     String sId = timer.getOID().getValue(0).toString();
                     TransferObject properties = new TransferObject(3);

                     properties.setValue("timer", sId);
                     properties.setValue(JMSSender.PROTECTED, Boolean.FALSE);

                     Object user = timer.getValue("principal");

                     if (user == null)
                     {
                        user = m_sDefaultUser;
                     }

                     if (user != null)
                     {
                        properties.setValue(JMSSender.USER, user);
                     }

                     if (s_logger.isDebugEnabled())
                     {
                        s_logger.debug("Timeout in timer \"" +  m_thisComponent.getName() +
                           "\", id=" + sId + "; forwarding to the message queue");
                     }

                     TransferObject tobj = new TransferObject(2);

                     tobj.setValue(JMSSender.BODY, ((Binary)timer.getValue("data")).toObject());
                     tobj.setValue(JMSSender.PROPERTIES, properties);

                     if (sendList == null)
                     {
                        sendList = new ArrayList(m_nBatchCount);
                     }

                     sendList.add(tobj);

                     long lPeriod = ((Number)timer.getValue("period")).longValue();

                     if (lPeriod <= 0)
                     {
                        timer.invoke("delete");
                     }
                     else
                     {
                        Timestamp tmStart = (Timestamp)timer.getValue("start");

                        lNextTimeout = tmStart.getTime() + lPeriod * ((lCurrentTime - tmStart.getTime()) / lPeriod + 1);
                        timer.setValue("next", new Timestamp(lNextTimeout));
                        reset(lNextTimeout, false);
                     }
                  }
                  else
                  {
View Full Code Here

         public void run(InvocationContext context) throws Throwable
         {
            context.setSecure(false);

            Metaclass dispatcherClass = m_metadata.getMetaclass("SysObjectQueueDispatcher");
            Instance dispatcher = (Instance)dispatcherClass.invoke("getDispatcher", new Object[]
            {
               Boolean.TRUE
            });

            m_sQueueName = (String)dispatcher.getValue("QUEUE_NAME");
            m_semaphoreMap = new HashTab();
            m_blockingSet = new HashHolder();
            m_nodeByProcessingMessageIdMap = new HashTab();
            m_processingMessagesByNodeNameMap = new HashTab();
            m_messageLoadAttributes = (Pair)dispatcher.getValue("SELECT_ATTRIBUTES", "LOAD_ATTRIBUTES");

            if (startNode(result, dispatcher, bReset, context))
            {
               s_logger.debug("This node selected to be dispatcher, recovering incomplete transactions");
               dispatcherClass.invoke("recover", new Object[]
View Full Code Here

                     if (deliverable != null && !deliverable.isEmpty())
                     {
                        for (int i = 0; i < deliverable.size(); i++)
                        {
                           final Instance message = deliverable.getInstance(i);
                           final Instance queue = (Instance)message.getValue("queue");
                           final String sQueueName = (String)queue.getValue("name");
                           Number priority = (Number)queue.getValue("priority");

                           pushMessage((priority == null) ? 1000 : priority.intValue(), i, sDispatcherId, (sQueueName
                              .equals(m_sQueueName)) ? ObjectDispatchListener.DISPATCHER : ObjectDispatchListener.ANY,
                              new DispatcherMessage()
                              {
View Full Code Here

      {
         s_logger.debug("Delivery failure for message with oid " + oid + ", calling fail", t);
      }

      Metaclass dispatcherClass = m_metadata.getMetaclass("SysObjectQueueDispatcher");
      Instance msg = readMessage(oid, m_messageLoadAttributes, context);

      if (msg == null)
      {
         return; // message has been canceled. Unlikely, but possible.
      }

      // login the message sender
      context.login(new SimplePrincipal((String)msg.getValue("user")));

      // check that message has not been recovered by the dispatcher
      if (!((Boolean)msg.findValue("isProcessing", Boolean.TRUE)).booleanValue())
      {
         throw new InvalidDispatcherException(sDispatcherId);
      }

      // invoke fail method
      msg.invoke("fail", new Object[] {t});

      boolean bSecured = context.isSecure();

      try
      {
View Full Code Here

      {
         Metaclass dispatcherClass = m_metadata.getMetaclass("SysObjectQueueDispatcher");
         InstanceList messages = Query.createRead(m_metadata.getMetaclass("SysMessage"), m_messageLoadAttributes, Pair.attribute("").eq(oid),
            null, -1, 0, false, Query.SEC_NONE, context).read();

         Instance msg = (messages == null || messages.isEmpty()) ? null : messages.getInstance(0);

         if (msg == null)
         {
            return; // message doesn't exist, can't blacklist it.
         }

         // check that message has not been recovered by the dispatcher
         if (!((Boolean)msg.findValue("isProcessing", Boolean.TRUE)).booleanValue())
         {
            throw new InvalidDispatcherException(sDispatcherId);
         }

         context.setSecure(false);
View Full Code Here

TOP

Related Classes of nexj.core.runtime.Instance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.