Ejemplo n.º 1
0
  @Override
  public void execute() throws TranslatorException {

    LogManager.logTrace(
        LogConstants.CTX_CONNECTOR,
        "ObjectExecution command:",
        query.toString(),
        "using connection:",
        connection.getClass().getName()); // $NON-NLS-1$ //$NON-NLS-2$

    String nameInSource =
        getNameInSource(((NamedTable) query.getFrom().get(0)).getMetadataObject());

    List<Object> results = factory.search(query, nameInSource, connection, executionContext);

    if (results != null && results.size() > 0) {
      LogManager.logDetail(
          LogConstants.CTX_CONNECTOR,
          "ObjectExecution number of returned objects is :",
          results.size()); // $NON-NLS-1$

    } else {
      LogManager.logDetail(
          LogConstants.CTX_CONNECTOR,
          "ObjectExecution number of objects returned is : 0"); //$NON-NLS-1$

      results = Collections.emptyList();
    }

    this.resultsIt = results.iterator();
  }
Ejemplo n.º 2
0
 TempTable getOrCreateTempTable(
     String tempTableID,
     Command command,
     BufferManager buffer,
     boolean delegate,
     boolean forUpdate,
     CommandContext context)
     throws TeiidProcessingException, BlockedException, TeiidComponentException {
   TempTable tempTable = getTempTable(tempTableID, command, buffer, delegate, forUpdate, context);
   if (tempTable != null) {
     if (processors != null) {
       TableProcessor withProcessor = processors.get(tempTableID);
       if (withProcessor != null) {
         buildWithTable(tempTableID, withProcessor, tempTable);
       }
     }
     return tempTable;
   }
   // allow implicit temp group definition
   List<ElementSymbol> columns = null;
   if (command instanceof Insert) {
     Insert insert = (Insert) command;
     GroupSymbol group = insert.getGroup();
     if (group.isImplicitTempGroupSymbol()) {
       columns = insert.getVariables();
     }
   }
   if (columns == null) {
     if (processors != null) {
       TableProcessor withProcessor = processors.get(tempTableID);
       if (withProcessor != null) {
         LogManager.logDetail(
             LogConstants.CTX_DQP,
             "Creating temporary table for with clause",
             tempTableID); //$NON-NLS-1$
         Create create = new Create();
         create.setTable(new GroupSymbol(tempTableID));
         create.setElementSymbolsAsColumns(withProcessor.columns);
         tempTable = addTempTable(tempTableID, create, buffer, true, context);
         buildWithTable(tempTableID, withProcessor, tempTable);
         return tempTable;
       }
     }
     throw new QueryProcessingException(
         QueryPlugin.Event.TEIID30226,
         QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30226, tempTableID));
   }
   LogManager.logDetail(
       LogConstants.CTX_DQP, "Creating temporary table", tempTableID); // $NON-NLS-1$
   Create create = new Create();
   create.setTable(new GroupSymbol(tempTableID));
   create.setElementSymbolsAsColumns(columns);
   return addTempTable(tempTableID, create, buffer, true, context);
 }
Ejemplo n.º 3
0
  private BasicSourceCapabilities buildCapabilities(ExecutionFactory<Object, Object> translator)
      throws TranslatorException {
    if (translator.isSourceRequiredForCapabilities()) {
      Object connection = null;
      Object connectionFactory = null;
      try {
        connectionFactory = getConnectionFactory();

        if (connectionFactory != null) {
          connection = translator.getConnection(connectionFactory, null);
        }
        if (connection == null) {
          throw new TranslatorException(
              QueryPlugin.Event.TEIID31108,
              QueryPlugin.Util.getString(
                  "datasource_not_found", getConnectionName())); // $NON-NLS-1$);
        }
        LogManager.logDetail(
            LogConstants.CTX_CONNECTOR,
            "Initializing the capabilities for",
            translatorName); //$NON-NLS-1$
        synchronized (executionFactory) {
          executionFactory.initCapabilities(connection);
        }
      } finally {
        if (connection != null) {
          translator.closeConnection(connection, connectionFactory);
        }
      }
    }
    BasicSourceCapabilities resultCaps = CapabilitiesConverter.convertCapabilities(translator, id);
    return resultCaps;
  }
 @Override
 public void cancel() throws TranslatorException {
   LogManager.logDetail(
       LogConstants.CTX_CONNECTOR,
       SpreadsheetExecutionFactory.UTIL.getString("cancel_query")); // $NON-NLS-1$
   this.rowIterator = null;
 }
Ejemplo n.º 5
0
  public ConnectorWork registerRequest(AtomicRequestMessage message)
      throws TeiidComponentException {
    checkStatus();
    AtomicRequestID atomicRequestId = message.getAtomicRequestID();
    LogManager.logDetail(
        LogConstants.CTX_CONNECTOR, new Object[] {atomicRequestId, "Create State"}); // $NON-NLS-1$

    final ConnectorWorkItem item = new ConnectorWorkItem(message, this);
    ConnectorWork proxy =
        (ConnectorWork)
            Proxy.newProxyInstance(
                ConnectorWork.class.getClassLoader(),
                new Class[] {ConnectorWork.class},
                new InvocationHandler() {
                  @Override
                  public Object invoke(Object proxy, Method method, Object[] args)
                      throws Throwable {
                    ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
                    try {
                      Thread.currentThread()
                          .setContextClassLoader(getExecutionFactory().getClass().getClassLoader());
                      return method.invoke(item, args);
                    } catch (InvocationTargetException e) {
                      throw e.getTargetException();
                    } finally {
                      Thread.currentThread().setContextClassLoader(originalCL);
                    }
                  }
                });

    Assertion.isNull(
        requestStates.put(atomicRequestId, proxy), "State already existed"); // $NON-NLS-1$
    return proxy;
  }
Ejemplo n.º 6
0
 public void send(Message message, Serializable messageKey) {
   message.setMessageKey(messageKey);
   if (LogManager.isMessageToBeRecorded(LogConstants.CTX_TRANSPORT, MessageLevel.DETAIL)) {
     LogManager.logDetail(LogConstants.CTX_TRANSPORT, "send message: " + message); // $NON-NLS-1$
   }
   objectSocket.write(message);
 }
 public UsernameTokenProfile(String user, String passwd, boolean encryptedPassword) {
   this.encryptedPassword = encryptedPassword;
   this.passwd = passwd;
   this.user = user;
   LogManager.logDetail(
       WSManagedConnectionFactory.UTIL.getString("using_username_profile")); // $NON-NLS-1$
 }
Ejemplo n.º 8
0
 @Override
 public CountResponse executeCount(Query query, List<SQLParam> parameters) {
   ConnectionImpl connection = null;
   try {
     String sql = query.toString();
     LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$
     connection = getConnection();
     final PreparedStatementImpl stmt = connection.prepareStatement(sql);
     if (!parameters.isEmpty()) {
       for (int i = 0; i < parameters.size(); i++) {
         stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType);
       }
     }
     ResultSet rs = stmt.executeQuery();
     rs.next();
     int count = rs.getInt(1);
     rs.close();
     stmt.close();
     return Responses.count(count);
   } catch (Exception e) {
     throw new ServerErrorException(e.getMessage(), e);
   } finally {
     try {
       if (connection != null) {
         connection.close();
       }
     } catch (SQLException e) {
     }
   }
 }
Ejemplo n.º 9
0
 /**
  * Close processing and clean everything up. Should only be called by the same thread that called
  * process.
  */
 public void closeProcessing() {
   if (processorClosed) {
     return;
   }
   if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
     LogManager.logDetail(
         LogConstants.CTX_DQP, "QueryProcessor: closing processor"); // $NON-NLS-1$
   }
   this.context.getTupleSourceCache().close();
   this.bufferMgr.releaseBuffers(reserved);
   reserved = 0;
   processorClosed = true;
   if (initialized) {
     try {
       processPlan.close();
     } catch (TeiidComponentException e1) {
       LogManager.logDetail(LogConstants.CTX_DQP, e1, "Error closing processor"); // $NON-NLS-1$
     }
   }
 }
Ejemplo n.º 10
0
 private void processMessagePacket(Message packet) {
   if (LogManager.isMessageToBeRecorded(LogConstants.CTX_TRANSPORT, MessageLevel.DETAIL)) {
     LogManager.logDetail(
         LogConstants.CTX_TRANSPORT, "processing message:" + packet); // $NON-NLS-1$
   }
   if (this.workContext.getSecurityHelper() != null) {
     this.workContext.getSecurityHelper().clearSecurityContext();
   }
   final ServerWorkItem work = new ServerWorkItem(this, packet.getMessageKey(), packet, this.csr);
   this.workContext.runInContext(work);
 }
Ejemplo n.º 11
0
  public void stop() {
    LogManager.logDetail(
        LogConstants.CTX_DQP, "Stopping BufferManager using", bufferDir); // $NON-NLS-1$
    bufferMgr.shutdown();

    // Delete the buffer directory
    if (bufferDir != null) {
      cleanDirectory(bufferDir);
      bufferDir.delete();
    }
  }
Ejemplo n.º 12
0
 private void buildAggregate(List<DBObject> query, String type, Object object) {
   if (object != null) {
     LogManager.logDetail(
         LogConstants.CTX_CONNECTOR,
         "{\""
             + type
             + "\": {"
             + object.toString()
             + "}}"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
     query.add(new BasicDBObject(type, object));
   }
 }
Ejemplo n.º 13
0
 public void remove() {
   if (!removed) {
     if (LogManager.isMessageToBeRecorded(LogConstants.CTX_BUFFER_MGR, MessageLevel.DETAIL)) {
       LogManager.logDetail(
           LogConstants.CTX_BUFFER_MGR,
           "Removing TupleBuffer:",
           this.tupleSourceID); // $NON-NLS-1$
     }
     this.batchBuffer = null;
     purge();
     this.manager.remove();
     removed = true;
   }
 }
Ejemplo n.º 14
0
 /**
  * Closes a connection object from the given connection factory.
  *
  * <p>The default implementation assumes a JCA {@link Connection}. Subclasses should override, if
  * they use another type of connection.
  *
  * @param connection
  * @param factory
  */
 public void closeConnection(C connection, F factory) {
   if (connection == null) {
     return;
   }
   if (connection instanceof Connection) {
     try {
       ((Connection) connection).close();
     } catch (ResourceException e) {
       LogManager.logDetail(LogConstants.CTX_CONNECTOR, e, "Error closing"); // $NON-NLS-1$
     }
     return;
   }
   throw new AssertionError(
       "A connection was created, but no implementation provided for closeConnection"); //$NON-NLS-1$
 }
Ejemplo n.º 15
0
  @Override
  public UpdateResponse executeUpdate(Command query, List<SQLParam> parameters) {
    ConnectionImpl connection = null;
    try {
      String sql = query.toString();
      LogManager.logDetail(LogConstants.CTX_ODATA, "Teiid-Query:", sql); // $NON-NLS-1$
      connection = getConnection();
      final PreparedStatementImpl stmt =
          connection.prepareStatement(
              sql,
              ResultSet.TYPE_FORWARD_ONLY,
              ResultSet.CONCUR_READ_ONLY,
              ResultSet.HOLD_CURSORS_OVER_COMMIT,
              Statement.RETURN_GENERATED_KEYS);
      if (!parameters.isEmpty()) {
        for (int i = 0; i < parameters.size(); i++) {
          stmt.setObject(i + 1, parameters.get(i).value, parameters.get(i).sqlType);
        }
      }
      final int count = stmt.executeUpdate();
      final Map<String, Object> keys = getGeneratedKeys(stmt.getGeneratedKeys());
      stmt.close();
      return new UpdateResponse() {
        @Override
        public Map<String, Object> getGeneratedKeys() {
          return keys;
        }

        @Override
        public int getUpdateCount() {
          return count;
        }
      };
    } catch (Exception e) {
      throw new ServerErrorException(e.getMessage(), e);
    } finally {
      try {
        if (connection != null) {
          connection.close();
        }
      } catch (SQLException e) {
      }
    }
  }
Ejemplo n.º 16
0
 public <T> Dispatch<T> createDispatch(Class<T> type, Mode mode) throws IOException {
   if (this.wsdlService == null) {
     Bus bus = BusFactory.getThreadDefaultBus();
     BusFactory.setThreadDefaultBus(this.mcf.getBus());
     try {
       this.wsdlService = Service.create(this.mcf.getWsdlUrl(), this.mcf.getServiceQName());
     } finally {
       BusFactory.setThreadDefaultBus(bus);
     }
     if (LogManager.isMessageToBeRecorded(LogConstants.CTX_WS, MessageLevel.DETAIL)) {
       LogManager.logDetail(
           LogConstants.CTX_WS, "Created the WSDL service for", this.mcf.getWsdl()); // $NON-NLS-1$
     }
   }
   Dispatch<T> dispatch = this.wsdlService.createDispatch(this.mcf.getPortQName(), type, mode);
   configureWSSecurity(dispatch);
   setDispatchProperties(dispatch, "SOAP12"); // $NON-NLS-1$
   return dispatch;
 }
Ejemplo n.º 17
0
  /**
   * @param port
   * @param inputBufferSize
   * @param outputBufferSize
   * @param engine null if SSL is disabled
   * @param bindaddress
   * @param server
   */
  public SocketListener(
      InetSocketAddress address,
      int inputBufferSize,
      int outputBufferSize,
      int maxWorkers,
      SSLConfiguration config,
      ClientServiceRegistryImpl csr,
      StorageManager storageManager) {
    if (config != null) {
      this.isClientEncryptionEnabled = config.isClientEncryptionEnabled();
    }
    this.csr = csr;

    this.nettyPool = Executors.newCachedThreadPool(new NamedThreadFactory("NIO")); // $NON-NLS-1$
    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_TRANSPORT, MessageLevel.DETAIL)) {
      LogManager.logDetail(
          LogConstants.CTX_TRANSPORT,
          "server = "
              + address.getAddress()
              + "binding to port:"
              + address.getPort()); // $NON-NLS-1$ //$NON-NLS-2$
    }

    if (maxWorkers == 0) {
      maxWorkers = Runtime.getRuntime().availableProcessors();
    }

    ChannelFactory factory =
        new NioServerSocketChannelFactory(this.nettyPool, this.nettyPool, maxWorkers);

    ServerBootstrap bootstrap = new ServerBootstrap(factory);
    this.channelHandler = createChannelPipelineFactory(config, storageManager);
    bootstrap.setPipelineFactory(channelHandler);
    if (inputBufferSize != 0) {
      bootstrap.setOption("receiveBufferSize", new Integer(inputBufferSize)); // $NON-NLS-1$
    }
    if (outputBufferSize != 0) {
      bootstrap.setOption("sendBufferSize", new Integer(outputBufferSize)); // $NON-NLS-1$
    }
    bootstrap.setOption("keepAlive", Boolean.TRUE); // $NON-NLS-1$

    this.serverChanel = bootstrap.bind(address);
  }
Ejemplo n.º 18
0
 public void close() throws TeiidComponentException {
   while (!programs.isEmpty()) {
     try {
       pop(false);
     } catch (TeiidComponentException e) {
       LogManager.logDetail(LogConstants.CTX_DQP, e, "Error closing program"); // $NON-NLS-1$
     }
   }
   if (this.evaluator != null) {
     this.evaluator.close();
   }
   if (this.cursorStates != null) {
     removeAllCursors(this.cursorStates);
     this.cursorStates = null;
   }
   this.txnTupleSources.clear();
   this.blockContext = null;
   this.currentVarContext = null;
 }
Ejemplo n.º 19
0
 public SocketListener(
     InetSocketAddress address,
     SocketConfiguration config,
     ClientServiceRegistryImpl csr,
     StorageManager storageManager) {
   this(
       address,
       config.getInputBufferSize(),
       config.getOutputBufferSize(),
       config.getMaxSocketThreads(),
       config.getSSLConfiguration(),
       csr,
       storageManager);
   LogManager.logDetail(
       LogConstants.CTX_TRANSPORT,
       RuntimePlugin.Util.getString(
           "SocketTransport.1",
           new Object[] {
             config.getHostAddress().getHostAddress(), String.valueOf(config.getPortNumber())
           })); //$NON-NLS-1$
 }
 @Override
 public void close() {
   LogManager.logDetail(
       LogConstants.CTX_CONNECTOR,
       SpreadsheetExecutionFactory.UTIL.getString("close_query")); // $NON-NLS-1$
 }
Ejemplo n.º 21
0
  private static void buildEntityTypes(
      MetadataStore metadataStore, List<EdmSchema.Builder> edmSchemas) {
    for (Schema schema : metadataStore.getSchemaList()) {

      List<EdmEntitySet.Builder> entitySets = new ArrayList<EdmEntitySet.Builder>();
      List<EdmEntityType.Builder> entityTypes = new ArrayList<EdmEntityType.Builder>();

      for (Table table : schema.getTables().values()) {

        KeyRecord primaryKey = table.getPrimaryKey();
        List<KeyRecord> uniques = table.getUniqueKeys();
        if (primaryKey == null && uniques.isEmpty()) {
          LogManager.logDetail(
              LogConstants.CTX_ODATA,
              ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16002, table.getFullName()));
          continue;
        }

        EdmEntityType.Builder entityType =
            EdmEntityType.newBuilder().setName(table.getName()).setNamespace(schema.getName());

        // adding key
        if (primaryKey != null) {
          for (Column c : primaryKey.getColumns()) {
            entityType.addKeys(c.getName());
          }
        } else {
          for (Column c : uniques.get(0).getColumns()) {
            entityType.addKeys(c.getName());
          }
        }

        // adding properties
        for (Column c : table.getColumns()) {
          EdmProperty.Builder property =
              EdmProperty.newBuilder(c.getName())
                  .setType(ODataTypeManager.odataType(c.getDatatype().getRuntimeTypeName()))
                  .setNullable(c.getNullType() == NullType.Nullable);
          if (c.getDatatype()
              .getRuntimeTypeName()
              .equals(DataTypeManager.DefaultDataTypes.STRING)) {
            property.setFixedLength(c.isFixedLength()).setMaxLength(c.getLength()).setUnicode(true);
          }
          entityType.addProperties(property);
        }

        // entity set one for one entity type
        EdmEntitySet.Builder entitySet =
            EdmEntitySet.newBuilder().setName(table.getName()).setEntityType(entityType);

        entityType.setNamespace(schema.getName());
        entitySets.add(entitySet);

        // add enitity types for entity schema
        entityTypes.add(entityType);
      }

      // entity container is holder entity sets, association sets, function imports
      EdmEntityContainer.Builder entityContainer =
          EdmEntityContainer.newBuilder()
              .setName(schema.getName())
              .setIsDefault(false)
              .addEntitySets(entitySets);

      // build entity schema
      EdmSchema.Builder modelSchema =
          EdmSchema.newBuilder()
              .setNamespace(schema.getName())
              .addEntityTypes(entityTypes)
              .addEntityContainers(entityContainer);

      edmSchemas.add(modelSchema);
    }
  }
Ejemplo n.º 22
0
 /**
  * initialize this <code>ConnectorManager</code>.
  *
  * @throws TranslatorException
  */
 public void start() {
   LogManager.logDetail(
       LogConstants.CTX_CONNECTOR,
       QueryPlugin.Util.getString(
           "ConnectorManagerImpl.Initializing_connector", translatorName)); // $NON-NLS-1$
 }
Ejemplo n.º 23
0
  private TupleSource handleCachedProcedure(final CommandContext context, StoredProcedure proc)
      throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
    String fullName = context.getMetadata().getFullName(proc.getProcedureID());
    LogManager.logDetail(
        LogConstants.CTX_DQP, "processing cached procedure request for", fullName); // $NON-NLS-1$
    LinkedList<Object> vals = new LinkedList<Object>();
    for (SPParameter param : proc.getInputParameters()) {
      vals.add(((Constant) param.getExpression()).getValue());
    }
    // collapse the hash to single byte for the key to restrict the possible results to 256
    int hash = vals.hashCode();
    hash |= (hash >>> 16);
    hash |= (hash >>> 8);
    hash &= 0x000000ff;
    final CacheID cid =
        new CacheID(
            new ParseInfo(),
            fullName + hash,
            context.getVdbName(),
            context.getVdbVersion(),
            context.getConnectionId(),
            context.getUserName());
    cid.setParameters(vals);
    CachedResults results = cache.get(cid);
    if (results != null) {
      TupleBuffer buffer = results.getResults();
      return buffer.createIndexedTupleSource();
    }
    // construct a query with a no cache hint
    final CacheHint hint = proc.getCacheHint();
    proc.setCacheHint(null);
    Option option = new Option();
    option.setNoCache(true);
    option.addNoCacheGroup(fullName);
    proc.setOption(option);
    StoredProcedure cloneProc = (StoredProcedure) proc.clone();
    int i = 0;
    for (SPParameter param : cloneProc.getInputParameters()) {
      param.setExpression(new Reference(i++));
    }
    final QueryProcessor qp =
        context
            .getQueryProcessorFactory()
            .createQueryProcessor(
                cloneProc.toString(), fullName.toUpperCase(), context, vals.toArray());
    final BatchCollector bc = qp.createBatchCollector();

    return new ProxyTupleSource() {
      boolean success = false;

      @Override
      protected TupleSource createTupleSource()
          throws TeiidComponentException, TeiidProcessingException {
        TupleBuffer tb = bc.collectTuples();
        CachedResults cr = new CachedResults();
        cr.setResults(tb, qp.getProcessorPlan());
        Determinism determinismLevel = qp.getContext().getDeterminismLevel();
        if (hint != null && hint.getDeterminism() != null) {
          LogManager.logTrace(
              LogConstants.CTX_DQP,
              new Object[] {
                "Cache hint modified the query determinism from ",
                determinismLevel,
                " to ",
                hint.getDeterminism()
              }); //$NON-NLS-1$ //$NON-NLS-2$
          determinismLevel = hint.getDeterminism();
        }
        cache.put(cid, determinismLevel, cr, hint != null ? hint.getTtl() : null);
        context.setDeterminismLevel(determinismLevel);
        success = true;
        return tb.createIndexedTupleSource();
      }

      @Override
      public void closeSource() {
        super.closeSource();
        qp.closeProcessing();
        if (!success && bc.getTupleBuffer() != null) {
          bc.getTupleBuffer().remove();
        }
      }
    };
  }
Ejemplo n.º 24
0
  private TupleSource handleSystemProcedures(final CommandContext context, StoredProcedure proc)
      throws TeiidComponentException, QueryMetadataException, QueryProcessingException,
          QueryResolverException, QueryValidatorException, TeiidProcessingException,
          ExpressionEvaluationException {
    final QueryMetadataInterface metadata = context.getMetadata();
    if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEW)) {
      Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      String matViewName = metadata.getFullName(groupID);
      String matTableName = metadata.getFullName(matTableId);
      LogManager.logDetail(
          LogConstants.CTX_MATVIEWS, "processing refreshmatview for", matViewName); // $NON-NLS-1$
      boolean invalidate =
          Boolean.TRUE.equals(((Constant) proc.getParameter(3).getExpression()).getValue());
      boolean needsLoading =
          globalStore.needsLoading(matTableName, globalStore.getAddress(), true, true, invalidate);
      if (!needsLoading) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
      }
      GroupSymbol matTable = new GroupSymbol(matTableName);
      matTable.setMetadataID(matTableId);
      return loadGlobalTable(context, matTable, matTableName, globalStore);
    } else if (StringUtil.endsWithIgnoreCase(proc.getProcedureCallableName(), REFRESHMATVIEWROW)) {
      final Object groupID =
          validateMatView(
              metadata, (String) ((Constant) proc.getParameter(2).getExpression()).getValue());
      TempMetadataID matTableId =
          context.getGlobalTableStore().getGlobalTempTableMetadataId(groupID);
      final GlobalTableStore globalStore = getGlobalStore(context, matTableId);
      Object pk = metadata.getPrimaryKey(groupID);
      String matViewName = metadata.getFullName(groupID);
      if (pk == null) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30230,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30230, matViewName));
      }
      List<?> ids = metadata.getElementIDsInKey(pk);
      Constant key = (Constant) proc.getParameter(3).getExpression();
      Object initialValue = key.getValue();
      SPParameter keyOther = proc.getParameter(4);
      Object[] otherCols = null;
      int length = 1;
      if (keyOther != null) {
        otherCols = ((ArrayImpl) ((Constant) keyOther.getExpression()).getValue()).getValues();
        if (otherCols != null) {
          length += otherCols.length;
        }
      }
      if (ids.size() != length) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30231,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30231, matViewName, ids.size(), length));
      }
      final String matTableName = RelationalPlanner.MAT_PREFIX + matViewName.toUpperCase();
      MatTableInfo info = globalStore.getMatTableInfo(matTableName);
      if (!info.isValid()) {
        return CollectionTupleSource.createUpdateCountTupleSource(-1);
      }
      TempTable tempTable = globalStore.getTempTable(matTableName);
      if (!tempTable.isUpdatable()) {
        throw new QueryProcessingException(
            QueryPlugin.Event.TEIID30232,
            QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30232, matViewName));
      }
      Iterator<?> iter = ids.iterator();
      final Object[] params = new Object[length];
      StringBuilder criteria = new StringBuilder();
      for (int i = 0; i < length; i++) {
        Object id = iter.next();
        String targetTypeName = metadata.getElementType(id);
        Object value = i == 0 ? initialValue : otherCols[i - 1];
        value =
            DataTypeManager.transformValue(value, DataTypeManager.getDataTypeClass(targetTypeName));
        params[i] = value;
        if (i != 0) {
          criteria.append(" AND "); // $NON-NLS-1$
        }
        criteria.append(metadata.getFullName(id)).append(" = ?"); // $NON-NLS-1$
      }
      LogManager.logInfo(
          LogConstants.CTX_MATVIEWS,
          QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30012, matViewName, Arrays.toString(params)));

      String queryString =
          Reserved.SELECT
              + " * "
              + Reserved.FROM
              + ' '
              + matViewName
              + ' '
              + Reserved.WHERE
              + ' '
              + //$NON-NLS-1$
              criteria.toString()
              + ' '
              + Reserved.OPTION
              + ' '
              + Reserved.NOCACHE;
      final QueryProcessor qp =
          context
              .getQueryProcessorFactory()
              .createQueryProcessor(queryString, matViewName.toUpperCase(), context, params);
      final TupleSource ts = new BatchCollector.BatchProducerTupleSource(qp);
      return new ProxyTupleSource() {

        @Override
        protected TupleSource createTupleSource()
            throws TeiidComponentException, TeiidProcessingException {
          List<?> tuple = ts.nextTuple();
          boolean delete = false;
          if (tuple == null) {
            delete = true;
            tuple = Arrays.asList(params);
          } else {
            tuple = new ArrayList<Object>(tuple); // ensure the list is serializable
          }
          List<?> result = globalStore.updateMatViewRow(matTableName, tuple, delete);
          if (eventDistributor != null) {
            eventDistributor.updateMatViewRow(
                context.getVdbName(),
                context.getVdbVersion(),
                metadata.getName(metadata.getModelID(groupID)),
                metadata.getName(groupID),
                tuple,
                delete);
          }
          return CollectionTupleSource.createUpdateCountTupleSource(result != null ? 1 : 0);
        }

        @Override
        public void closeSource() {
          super.closeSource();
          qp.closeProcessing();
        }
      };
    }
    return null;
  }
Ejemplo n.º 25
0
 /** Remove the state associated with the given <code>RequestID</code>. */
 boolean removeState(AtomicRequestID sid) {
   LogManager.logDetail(LogConstants.CTX_CONNECTOR, sid, "Remove State"); // $NON-NLS-1$
   return requestStates.remove(sid) != null;
 }
Ejemplo n.º 26
0
 TempTable getOrCreateTempTable(
     String tempTableID,
     Command command,
     BufferManager buffer,
     boolean delegate,
     boolean forUpdate,
     CommandContext context,
     GroupSymbol group)
     throws TeiidProcessingException, BlockedException, TeiidComponentException {
   if (!(group.getMetadataID() instanceof TempMetadataID)) {
     // TODO: use a proper metadata
     TempTableStore tts = context.getSessionTempTableStore();
     context.setDeterminismLevel(Determinism.SESSION_DETERMINISTIC);
     if (tts.getTempTable(tempTableID) == null) {
       // implicitly create global (session scoped) temp table
       LogManager.logDetail(
           LogConstants.CTX_DQP, "binding global temp table to session", group); // $NON-NLS-1$
       QueryMetadataInterface metadata = context.getMetadata();
       Create create = GlobalTableStoreImpl.getCreateCommand(group, false, metadata);
       tts.addTempTable(tempTableID, create, buffer, true, context);
     }
     return getTempTable(tempTableID, command, buffer, delegate, forUpdate, context);
   }
   TempTable tempTable = getTempTable(tempTableID, command, buffer, delegate, forUpdate, context);
   if (tempTable != null) {
     if (processors != null) {
       TableProcessor withProcessor = processors.get(tempTableID);
       if (withProcessor != null) {
         TempTable tt = withProcessor.process(tempTable);
         if (tt != tempTable) {
           return tt;
         }
         processors.remove(tempTableID);
       }
     }
     return tempTable;
   }
   // allow implicit temp group definition
   List<ElementSymbol> columns = null;
   if (command instanceof Insert) {
     Insert insert = (Insert) command;
     if (group.isImplicitTempGroupSymbol()) {
       columns = insert.getVariables();
     }
   }
   if (columns == null) {
     if (processors != null) {
       TableProcessor withProcessor = processors.get(tempTableID);
       if (withProcessor != null) {
         LogManager.logDetail(
             LogConstants.CTX_DQP,
             "Creating temporary table for with clause",
             tempTableID); //$NON-NLS-1$
         Create create = new Create();
         create.setTable(new GroupSymbol(tempTableID));
         create.setElementSymbolsAsColumns(withProcessor.columns);
         withProcessor.alterCreate(create);
         tempTable = addTempTable(tempTableID, create, buffer, true, context);
         TempTable tt = withProcessor.process(tempTable);
         if (tt != tempTable) {
           return tt;
         }
         processors.remove(tempTableID);
         return tempTable;
       }
     }
     if (delegate && this.parentTempTableStore != null) {
       // may be a cte from a higher scope that needs to have creation triggered
       return parentTempTableStore.getOrCreateTempTable(
           tempTableID, command, buffer, delegate, forUpdate, context, group);
     }
     throw new QueryProcessingException(
         QueryPlugin.Event.TEIID30226,
         QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30226, tempTableID));
   }
   LogManager.logDetail(
       LogConstants.CTX_DQP, "Creating temporary table", tempTableID); // $NON-NLS-1$
   Create create = new Create();
   create.setTable(new GroupSymbol(tempTableID));
   create.setElementSymbolsAsColumns(columns);
   return addTempTable(tempTableID, create, buffer, true, context);
 }
Ejemplo n.º 27
0
  @Override
  public LogonResult neogitiateGssLogin(
      Properties connProps, byte[] serviceTicket, boolean createSession) throws LogonException {

    if (!AuthenticationType.GSS.equals(service.getAuthenticationType())) {
      throw new LogonException(
          RuntimePlugin.Event.TEIID40055,
          RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40055, "Kerberos")); // $NON-NLS-1$
    }

    String user = connProps.getProperty(TeiidURL.CONNECTION.USER_NAME);
    String password = connProps.getProperty(TeiidURL.CONNECTION.PASSWORD);
    Object previous = null;
    boolean associated = false;
    try {
      String securityDomain = service.getGssSecurityDomain();
      if (securityDomain == null) {
        throw new LogonException(
            RuntimePlugin.Event.TEIID40059, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40059));
      }
      // If this KRB5 and using keytab, user and password callback handler never gets called
      LoginContext ctx = service.createLoginContext(securityDomain, user, password);
      ctx.login();
      Subject subject = ctx.getSubject();
      GSSResult result = Subject.doAs(subject, new GssAction(serviceTicket));
      if (result == null) {
        throw new LogonException(
            RuntimePlugin.Event.TEIID40014, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40014));
      }

      if (result.context.isEstablished()) {
        Principal principal = null;
        for (Principal p : subject.getPrincipals()) {
          principal = p;
          break;
        }
        SecurityHelper securityHelper = service.getSecurityHelper();
        Object securityContext =
            securityHelper.createSecurityContext(securityDomain, principal, null, subject);
        previous = securityHelper.associateSecurityContext(securityContext);
        associated = true;
      }

      if (!result.context.isEstablished() || !createSession) {
        LogonResult logonResult =
            new LogonResult(
                new SessionToken(0, "temp"),
                "internal",
                0,
                "internal"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        logonResult.addProperty(ILogon.KRB5TOKEN, result.serviceTicket);
        logonResult.addProperty(
            ILogon.KRB5_ESTABLISHED, new Boolean(result.context.isEstablished()));
        return logonResult;
      }

      LogManager.logDetail(
          LogConstants.CTX_SECURITY, "Kerberos context established"); // $NON-NLS-1$
      // connProps.setProperty(TeiidURL.CONNECTION.PASSTHROUGH_AUTHENTICATION, "true");
      // //$NON-NLS-1$
      LogonResult loginInResult = logon(connProps, result.serviceTicket);
      return loginInResult;
    } catch (LoginException e) {
      throw new LogonException(
          RuntimePlugin.Event.TEIID40014, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40014));
    } finally {
      if (associated) {
        service.getSecurityHelper().associateSecurityContext(previous);
      }
    }
  }
Ejemplo n.º 28
0
  public static SaxonXQueryExpression.Result evaluateXQuery(
      final SaxonXQueryExpression xquery,
      Object context,
      Map<String, Object> parameterValues,
      final RowProcessor processor,
      CommandContext commandContext)
      throws TeiidProcessingException, TeiidComponentException {
    DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config);

    SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result();
    try {
      try {
        for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
          Object value = entry.getValue();
          if (value instanceof SQLXML) {
            value = XMLSystemFunctions.convertToSource(value);
            result.sources.add((Source) value);
            value = wrapStax((Source) value, xquery.getConfig());
          } else if (value instanceof java.util.Date) {
            value = XMLSystemFunctions.convertToAtomicValue(value);
          } else if (value instanceof BinaryType) {
            value = new HexBinaryValue(((BinaryType) value).getBytesDirect());
          }
          dynamicContext.setParameter(entry.getKey(), value);
        }
      } catch (TransformerException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30148, e);
      }
      if (context != null) {
        Source source = XMLSystemFunctions.convertToSource(context);
        result.sources.add(source);
        source = wrapStax(source, xquery.getConfig());
        if (xquery.contextRoot != null) {
          // create our own filter as this logic is not provided in the free saxon
          ProxyReceiver filter = new PathMapFilter(xquery.contextRoot);
          AugmentedSource sourceInput = AugmentedSource.makeAugmentedSource(source);
          sourceInput.addFilter(filter);
          source = sourceInput;

          // use streamable processing instead
          if (xquery.streamingPath != null && processor != null) {
            if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
              LogManager.logDetail(
                  LogConstants.CTX_DQP,
                  "Using stream processing for evaluation of",
                  xquery.xQueryString); // $NON-NLS-1$
            }
            // set to non-blocking in case default expression evaluation blocks
            boolean isNonBlocking = commandContext.isNonBlocking();
            commandContext.setNonBlocking(true);

            final StreamingTransform myTransform =
                new StreamingTransform() {
                  public Nodes transform(Element elem) {
                    processor.processRow(XQueryEvaluator.wrap(elem, xquery.config));
                    return NONE;
                  }
                };

            Builder builder =
                new Builder(
                    new SaxonReader(xquery.config, sourceInput),
                    false,
                    new StreamingPathFilter(xquery.streamingPath, xquery.namespaceMap)
                        .createNodeFactory(null, myTransform));
            try {
              // the builder is hard wired to parse the source, but the api will throw an exception
              // if the stream is null
              builder.build(FAKE_IS);
              return result;
            } catch (ParsingException e) {
              if (e.getCause() instanceof TeiidRuntimeException) {
                RelationalNode.unwrapException((TeiidRuntimeException) e.getCause());
              }
              throw new TeiidProcessingException(
                  QueryPlugin.Event.TEIID30151,
                  e,
                  QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            } catch (IOException e) {
              throw new TeiidProcessingException(
                  QueryPlugin.Event.TEIID30151,
                  e,
                  QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            } finally {
              if (!isNonBlocking) {
                commandContext.setNonBlocking(false);
              }
            }
          }
        }
        DocumentInfo doc;
        try {
          doc = xquery.config.buildDocument(source);
        } catch (XPathException e) {
          throw new TeiidProcessingException(
              QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
        }
        dynamicContext.setContextItem(doc);
      }
      try {
        result.iter = xquery.xQuery.iterator(dynamicContext);
        return result;
      } catch (TransformerException e) {
        throw new TeiidProcessingException(
            QueryPlugin.Event.TEIID30152, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30152));
      }
    } finally {
      if (result.iter == null) {
        result.close();
      }
    }
  }
Ejemplo n.º 29
0
  public void start() {
    try {
      // Construct and initialize the buffer manager
      this.bufferMgr = new BufferManagerImpl();
      this.bufferMgr.setProcessorBatchSize(processorBatchSize);
      this.bufferMgr.setMaxReserveKB(this.maxReserveKb);
      this.bufferMgr.setMaxProcessingKB(this.maxProcessingKb);
      this.bufferMgr.setInlineLobs(inlineLobs);
      this.bufferMgr.initialize();

      // If necessary, add disk storage manager
      if (useDisk) {
        LogManager.logDetail(
            LogConstants.CTX_DQP, "Starting BufferManager using", bufferDir); // $NON-NLS-1$
        if (!bufferDir.exists()) {
          this.bufferDir.mkdirs();
        }
        // start the file storage manager in clean state
        // wise FileStorageManager is smart enough to clean up after itself
        cleanDirectory(bufferDir);
        // Get the properties for FileStorageManager and create.
        fsm = new FileStorageManager();
        fsm.setStorageDirectory(bufferDir.getCanonicalPath());
        fsm.setMaxOpenFiles(maxOpenFiles);
        fsm.setMaxBufferSpace(maxBufferSpace * MB);
        SplittableStorageManager ssm = new SplittableStorageManager(fsm);
        ssm.setMaxFileSize(maxFileSize);
        fsc = new BufferFrontedFileStoreCache();
        fsc.setMaxStorageObjectSize(maxStorageObjectSize);
        fsc.setDirect(memoryBufferOffHeap);
        int batchOverheadKB =
            (int)
                    (this.memoryBufferSpace < 0
                        ? (this.bufferMgr.getMaxReserveKB() << 8)
                        : this.memoryBufferSpace)
                >> 20;
        this.bufferMgr.setMaxReserveKB(
            Math.max(0, this.bufferMgr.getMaxReserveKB() - batchOverheadKB));
        if (memoryBufferSpace < 0) {
          // use approximately 25% of what's set aside for the reserved
          fsc.setMemoryBufferSpace(((long) this.bufferMgr.getMaxReserveKB()) << 8);
        } else {
          // scale from MB to bytes
          fsc.setMemoryBufferSpace(memoryBufferSpace << 20);
        }
        if (!memoryBufferOffHeap && this.maxReserveKb < 0) {
          // adjust the value
          this.bufferMgr.setMaxReserveKB(
              this.bufferMgr.getMaxReserveKB()
                  - (int)
                      Math.min(
                          this.bufferMgr.getMaxReserveKB(), (fsc.getMemoryBufferSpace() >> 10)));
        }
        fsc.setStorageManager(ssm);
        fsc.initialize();
        this.bufferMgr.setCache(fsc);
        this.workingMaxReserveKb = this.bufferMgr.getMaxReserveKB();
      } else {
        this.bufferMgr.setCache(new MemoryStorageManager());
      }

    } catch (TeiidComponentException e) {
      throw new TeiidRuntimeException(
          RuntimePlugin.Event.TEIID40039, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40039));
    } catch (IOException e) {
      throw new TeiidRuntimeException(
          RuntimePlugin.Event.TEIID40039, e, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40039));
    }
  }
Ejemplo n.º 30
0
  public <T> Dispatch<T> createDispatch(String binding, String endpoint, Class<T> type, Mode mode) {
    ArgCheck.isNotNull(binding);
    if (endpoint != null) {
      try {
        new URL(endpoint);
        // valid url, just use the endpoint
      } catch (MalformedURLException e) {
        // otherwise it should be a relative value
        // but we should still preserve the base path and query string
        String defaultEndpoint = this.mcf.getEndPoint();
        String defaultQueryString = null;
        String defaultFragment = null;
        if (defaultEndpoint == null) {
          throw new WebServiceException(
              WSManagedConnectionFactory.UTIL.getString("null_default_endpoint")); // $NON-NLS-1$
        }
        String[] parts = defaultEndpoint.split("\\?", 2); // $NON-NLS-1$
        defaultEndpoint = parts[0];
        if (parts.length > 1) {
          defaultQueryString = parts[1];
          parts = defaultQueryString.split("#"); // $NON-NLS-1$
          defaultQueryString = parts[0];
          if (parts.length > 1) {
            defaultFragment = parts[1];
          }
        }
        if (endpoint.startsWith("?") || endpoint.startsWith("/")) { // $NON-NLS-1$ //$NON-NLS-2$
          endpoint = defaultEndpoint + endpoint;
        } else {
          endpoint = defaultEndpoint + "/" + endpoint; // $NON-NLS-1$
        }
        if ((defaultQueryString != null) && (defaultQueryString.trim().length() > 0)) {
          endpoint = WSConnection.Util.appendQueryString(endpoint, defaultQueryString);
        }
        if ((defaultFragment != null) && (endpoint.indexOf('#') < 0)) {
          endpoint = endpoint + '#' + defaultFragment;
        }
      }
    } else {
      endpoint = this.mcf.getEndPoint();
      if (endpoint == null) {
        throw new WebServiceException(
            WSManagedConnectionFactory.UTIL.getString("null_endpoint")); // $NON-NLS-1$
      }
    }
    Dispatch<T> dispatch = null;
    if (HTTPBinding.HTTP_BINDING.equals(binding) && (type == DataSource.class)) {
      Bus bus = BusFactory.getThreadDefaultBus();
      BusFactory.setThreadDefaultBus(this.mcf.getBus());
      try {
        dispatch =
            (Dispatch<T>)
                new HttpDispatch(endpoint, this.mcf.getConfigFile(), this.mcf.getConfigName());
      } finally {
        BusFactory.setThreadDefaultBus(bus);
      }
    } else {
      // TODO: cache service/port/dispatch instances?
      Bus bus = BusFactory.getThreadDefaultBus();
      BusFactory.setThreadDefaultBus(this.mcf.getBus());
      Service svc;
      try {
        svc = Service.create(this.mcf.getServiceQName());
      } finally {
        BusFactory.setThreadDefaultBus(bus);
      }
      if (LogManager.isMessageToBeRecorded(LogConstants.CTX_WS, MessageLevel.DETAIL)) {
        LogManager.logDetail(
            LogConstants.CTX_WS, "Creating a dispatch with endpoint", endpoint); // $NON-NLS-1$
      }
      svc.addPort(this.mcf.getPortQName(), binding, endpoint);

      dispatch = svc.createDispatch(this.mcf.getPortQName(), type, mode);
      configureWSSecurity(dispatch);
    }
    setDispatchProperties(dispatch, binding);
    return dispatch;
  }